Sublime Forum

Advanced Snippet

#1

Detecting Class Names / Data Types

I’m trying to make my snippets “smarter”. I want to be able to show methods based off the class and be able to show appropriate methods for a specific data type. Example:

class MainApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        int i = 0;
        console.log(i.compareTo(Integer));
    }
}

For instance, I would like to show methods for System that are actually available for System. Also for the variable i, I would like to show snippets available only to Integer. Is there any possible way I can check the variable and what type it is, whether it be a class, data type, or some other data structure? It would improve snippets much more if it’s not yet possible. Does anyone know of any way to go about doing this? I assume I would have to analyze the grammar, via a lexical parser. Thanks in advance.

0 Likes

#2

This might be a start:

0 Likes

#3

How would I go able doing this for a programming language such as Java or JavaScript? When looking at the link you provided, it led me to the Java package from sublimehq and it seems they use a unique definition syntax for the .sublime-syntax file. Would I simply look for the scope that my class / data type fits into? If so, I found a clever command which detects the scope of the selected text.
EDIT: I think I know how to fix this; I just need to be able to check for a specific data type or class. For example, in C++, there exists a bool data type and a int data type. Is there any way to differentiate between the two? When checking the scope via:

view.scope_name(view.sel()[0].b)

Both data types return 'source.c++ meta.function.c++ meta.block.c++ '
Same goes for two different classes; every class instantiation will return source.c++ meta.function.c++ meta.block.c++ If I want to go about defining a snippet which triggers a method for a specific class, how can I do that?

0 Likes