7ute / symbols-list

An alternate symbol list sidebar for Atom.io text editor
GNU General Public License v3.0
32 stars 24 forks source link

Ruby: self is not the name of all class methods #26

Closed thorstenhirsch closed 7 years ago

thorstenhirsch commented 8 years ago

Ruby's class methods are defined like this: def self.bar; end But symbols-list only shows "self". That's the part that is fixed in the definition of class methods. The real name is what comes after "self" and the dot. It would also be nice to have a visual distinction between class methods and instance methods. Maybe different colors or different icons like [c] and [i].

mbuc82 commented 7 years ago

Hello @thorstenhirsch ,

of course this would be possible and i would like to improve the related regular expressions, but i'm not that familiar with ruby. Could you please provide a file including the code that needs to be covered by the symbols?

I would update the regexp (and maybe the current local test file) and come back to you afterwards.

Regards

thorstenhirsch commented 7 years ago

Hi @mbuc82 , sure, let's just use this example I found in a Ruby tutorial:

#!/usr/bin/env ruby
class Foo

  include SomeModule

  def self.bar
    puts 'class method'
  end

  def baz
    puts 'instance method'
  end
end

Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class

Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’ 

I guess we can focus on the class and just skip the 4 lines at the bottom. So we have a class definition and 2 methods - a class method and an instance method:

You can use the keyword "class" for searching class definitions. Method definitions begin with "def". The distinction between class methods and instance methods is that all class methods begin with "self.".

I'm not sure what to do with namespaces. They look like this:

class Namespace::Foo
  def self.bar
    puts 'class method'
  end
end

There's another way to define namespaces:

module Namespace
  class Foo
    def self.bar
      puts 'class method'
    end
  end
end

It means the same as the other namespace example, it's just a different way to implement it. But I guess we don't need the namespace in the symbols list at all. So just skip everything before the (optional) "::" to fetch the class name in your regex like this:

 /class (?:.*::)?(.+)/$1/
mbuc82 commented 7 years ago

Hello @thorstenhirsch ,

thank you very much for that detailed explanation! I'll update the source and let you know, when it's finished.

Regards

mbuc82 commented 7 years ago

Hello @thorstenhirsch ,

i've updated the regular expressions for Ruby, to differ between class methods and instance methods. I've also updated the existing ones to exclude the namespaces (in class definitions) and to fit the code better than before.

The improvements will be included within the upcoming release. Please let me know, if you face any further problems, until then i would close this issue.

Regards

thorstenhirsch commented 7 years ago

I just upgraded to 2.4.0 and it seems to work great. Good job! Thank you!