Sublime Forum

Smarter goto_definition command

#1

I’m developing in C++. It would be nice if the goto_definition command were a bit smarter. It has no ability to detect macros, member variables or class/global constants. It does not prefer “nearby” definitions of functions or classes.

In other words, goto_definition has no preference for a definition of a symbol if that definition is in the current file, and it has no preference for definitions of symbols that are in files in the same directory. This is a serious limitation; any time I want to do a symbol lookup for a common function name, like Connect() or something, I need to look through maybe dozens of unrelated hits before I find the relevant one, which is probably located in some relatively local file.

Unfortunately, despite these limitations, AFAIK, goto_symbol is pretty much the current best tool for symbol lookup, since the only CTags plugin for Sublime Text lacks a lot of features and has a rather limited interface. So it would be great if goto_definition were more customizable or did a little more work to prioritize local results (and worked with macros, member variables, and class/global constants).

0 Likes

#2

your point about preferring nearby definitions has also been raised here, in case you want to subscribe to it/up vote it:

0 Likes

#3

In terms of what elements are indexed, that is controlled by the combination of the .sublime-syntax and .tmPreferences files at https://github.com/sublimehq/Packages/tree/master/C%2B%2B.

Currently most of the default packages index functions and classes. I believe Scala has been enhanced by some of the community members to index even more.

The real issue is just walking the fine line of indexing enough, but not so much that it is too noisy to use.

0 Likes

#4

The Scala mode additionally indexes variables, simply because the Scala language doesn’t make a hard distinction between variables and functions, so Scala developers tend to use them interchangeably. Additionally, because Scala doesn’t impose scope restrictions on constructs (e.g. classes may be defined inside of functions), Scala indexes are naturally a lot larger since basically every non-parametric definition is eligible.

In practice this works pretty well, but it’s very definitely a noisier index than the equivalent Java, Python, C++ or JavaScript codebase. With C++, a harder line can be drawn since the language is more restrictive with definitions and doesn’t conflate constructs in the way that Scala does, so the index can be kept cleaner.

As Will said: it’s a balancing act.

0 Likes

#5

I copied Symbol List.tmPreferences and C++.sublime-syntax to a newly-created Packages/C++ folder and tried modifying the tmPreferences file, but it doesn’t seem to be working.

The current <string> value is
(source.c | source.c++ | source.objc | source.objc++) & (entity.name.class | entity.name.struct | entity.name.union | entity.name.enum)

I tried modifying it to
(source.c | source.c++ | source.objc | source.objc++) & (entity.name.class | entity.name.struct | entity.name.union | entity.name.enum | variable.other.readwrite.member)

to include member variables with m_ syntax, but no luck. Maybe this is because variable.other.readwrite.member.c++ doesn’t include any meta_content_scope?

0 Likes

#6

variable.other.readwrite.member is used in a few places, but mostly where the members are used in specific contexts. Did you confirm that you are seeing that scope applied to your class members?

Other than that, did you check to see if Sublime Text is done indexing your project? Whenever a .tmPreferences controlling indexing, or a .sublime-syntax file is changed, the index needs to be rebuilt. It may be that indexing hasn’t yet scanned the item you are looking for. See Help > Indexing Status… for details.

The other factor that may be in play here is that the Goto Symbol in Project command and the symbol hover popup are currently hardcoded to only deal with identifiers that are 3 or more characters in length.

0 Likes

#7

Yes, the variable.other.readwrite.member scope is applied to variables beginning with m_, both where they are defined and where I try to use them later (the scope is listed when I press Ctrl+Shift+Alt+P).

I don’t see Indexing Status listed under the Help menu. Sublime Text is definitely noticing the Packages/C++/Symbol List.tmPreferences file I created; for example, if I introduce garbage syntax in it, I get a popup message.

0 Likes

#8

What version of Sublime Text are you running?

0 Likes

#9

Sublime Text 3, build 3114.

0 Likes

#10

Build 3126 added the indexing status window.

0 Likes

#11

I can confirm those changes add all sorts of member variables to the index. However, I don’t think that is going to be what you want since it will now index every usage of the member variable, not just the member definition.

0 Likes

#12

I updated to build 3126.

No jobs are shown in the Indexing Status window after an edit to the symbol list file; status Idle. Presumably the updates are just made rather quickly since I’m working with currently open files rather than a Project directory. Anyway, the jobs aren’t getting stalled.

Can someone verify that the Symbol List.tmPreferences edit I mention above, to mention variable.other.readwrite.member, would be enough to make Sublime Text treat variables with the m_ prefix as symbols?

0 Likes

#13

Yes, I confirmed this works in the post above.

0 Likes