Is there a way to determine if an element at caret is a keyword for the given language?

已回答

In my plugin I would like to perform different actions when on a keyword as opposed to an identifier for the file’s language.

Each language already comes with the knowledge of its keywords, I am wondering if there is a way to query its ParserDefinition or Lexer for this information without needing to know specifics of the language?

The plugin is language agnostic but I would like to know if PsiElement at caret is a keyword and be able to do it in IntelliJ for different languages or in other IDEs like PhpStorm, WebStorm, PyCharm, etc.

For example when pasting over an identifier I will match identifier name style (SCREAMING_SNAKE_CASE, snake_case, camelCase, PascalCase, dot.case, dash-case, slash/case, space case, etc.) The same clipboard content can be used to replace multiple texts without requiring editing.

For example pasting over [some.action] in “some.action.family.name” with “MoveLineUpAction” on the clipboard will result in “move.line.up.action.family.name”. Conveniently creating a resource name from action’s class name.

When the same is done over a keyword, I would like to disable this matching and just paste the clipboard contents.

 

0

As you might know from your experience - there is no such entity in the platform as a keyword. There are spaces/newlines, comments, errors and everything else. Some languages might have something like that, but platform does not requires it. 

0

I was hoping I missed something.

There is sort of this information but it is fragmented across multiple "extensions" which may or may not be implemented and indirectly provides this information: LexerBasedTodoIndexer.advance(), SpellcheckingStrategy.getTokenizer(), FindUsagesProvider.getWordScanner().

I guess the alternative is to allow the user to teach the plugin what constitutes a keyword by adding selected text to the list and the plugin using the language PsiElement class/node element type to anticipate other keywords.

If I provide a mechanism for the users to share their keyword list then I can consolidate them by language and over time add it to the plugin's default configuration.

0

One thing you could do is create an extension point in your plugin allowing language plugin authors to supply the list of lexer tokens which represent keywords for a particular language. Then you could provide implementations for the standard languages and any others users request, either in your plugin or by providing pull requests for other language plugins.

0

Thanks Colin. It is a good idea to implement. I will go that route along with user definable keywords per language.

I think it is a weakness of the strength of the IDE that it does not need such things as keywords. It necessitates adding a ton of extensions for each occasion. The approach is most flexible but does require coding for every language and dialect. In edge cases the effort is not worth the gain.

0

请先登录再写评论。