Sublime Forum

C\C++ files identifying scopes not working correctly

#1

Hello,

When I open a c\c++ file sublime doesn’t understand the scope that the cursor is currently at
example

0 Likes

#2

What do you mean exactly by “doesn’t understand the scope”?

0 Likes

#3

as you can see in the bar here it displays the scope as

0 Likes

#4

The C++ syntax definition reuses some syntax context rule sets from the C syntax in order to avoid duplication (and similarly, Objective-C++ reuses some syntax context rule sets from the C++ syntax).

Visually this has no impact; the specificity of whether a comment appears in a C or C++ file is not given because generally speaking you want comments to be the same color regardless of the language that’s being used.

If you’re doing something like a key binding, snippet or completion that you only want to be available inside of a line comment in C++ in a mixed C and C++ environment, modify the scope that you’re using to include the source type that you want.

For example, in your code above, there are two things that are comments:

>>> view.find_by_selector('comment')
[Region(61, 76), Region(86, 102)]

There are also two things that are comments in a C++ file:

>>> view.find_by_selector('source.c++ comment')
[Region(61, 76), Region(86, 102)]

There are however no comments in a C file, because this is a C++ file:

>>> view.find_by_selector('source.c comment')
[]
0 Likes

#5

I’m not sure I’m following you, the problem isn’t related to comments, it’s that it can’t detect the scope name correctly so it can’t display it correctly instead it’s just showing these generic text.

0 Likes

#6

You’re going to have to explain more fully what you expect to see there then.

The scope that’s displayed is:

source.c++ 
meta.function.c++ 
meta.block.c++ 
meta.block.c++ 
comment.line.double-slash.c
  • source.c++: this is a file of type C++
  • meta.function.c++: The cursor is inside of a C++ function (main)
  • meta.block.c++: The cursor is inside of a block of code (the body of main)
  • meta.block.c++: The cursor is inside of a block of code (the body of the for in main)
  • comment.line.double-slash.c: The cursor is inside of a comment that is a line comment that was defined by two slashes, and is in a C file

This is exactly what I would expect out of the scope here (the only “weird” thing being that the comment says that it’s C and not C++, but as noted above for anything that cares that’s not actually an issue).

Potentially you may be expecting that plugin (looks like ScopeHunter maybe?) to tell you something like myFile.c++ > main > for or something; that’s not what it does and that’s not what scopes mean in Sublime.

In that case you might want the BreadCrumbs package, though it appears that may require you to write some language specific regular expressions to pull the “crumbs” out.

0 Likes

#7

I was indeed looking for something to show me myFile.c++ > main > for I will try to look for a package that does that then, thanks a lot for explaining what scopes mean in sublime, perhaps I can write a plugin that queries the info and display it in the way I wanted if I can’t find exactly what I want.

1 Like