How to add "rename" functionality for strings in Javascript

In JavaScript quite ofter I use strings as identifiers (for event names, module names etc.). I would like to create plugin which adds "rename" functionality for JavaScript strings, but looking at the documentation I don't see if it's possible. Is it? If it's possible where should I start?

Regards,
Mateusz

11 comments
Comment actions Permalink

Hello Mateusz,

the (include string results in search & replace) functionality may already be there. 

in the rename dialog (you may need to tweak settings so it pops up) there should be a checkbox "Search for Text Occurrences" or something like this.

if you decide to write a plugin, here is a place in code which may shed some light:

https://github.com/JetBrains/intellij-community/blob/210e0ed138627926e10094bb9c76026319cec178/platform/lang-impl/src/com/intellij/refactoring/rename/RenamePsiElementProcessor.java#L157 

0
Comment actions Permalink

Hi Imants,

Thank you for this answer. I've managed to implement my RenamePsiElementProcessor. 

The problem is that IntelliJ IDEA doesn't even show the rename dialog for strings and this is what I'd like to fix in my plugin. Please take a look at the following image:

I'm trying to introduce something similar to symbols in Ruby to Javascript. What I mean is to treat strings in format ":hyphen-separated" specially by marking them in a special way in the editor (this is already done - ':module-name' is blue while 'normal string' is green) and providing the "find usages" and "rename" functionality.

The problem is when I set my cursor on ':module-name' and press shift+F6 then nothing happends. When I press alt+F7 it says "Cannot search for usages".

The documentation says about implementing this functionality in a custom language but it doesn't mention extending JavaScript. Is JSLanguageDialect the way to go?

0
Comment actions Permalink

maybe try to add this Extension Point:

https://github.com/JetBrains/intellij-community/blob/210e0ed138627926e10094bb9c76026319cec178/platform/lang-api/src/com/intellij/refactoring/rename/RenameHandler.java#L30 

and make

isAvailableOnDataContext

return true for String (or whatever type you use for String identifiers)?

0
Comment actions Permalink

Thank you again for this answer! It seems that I will be able to implement rename functionality with RenameHandler. So the only thing missing is the "find usages" functionality.

I've tried to implement the lang.findUsagesProvider with language="JavaScript" but it seems that IntelliJ doesn't care about it. Perhaps I need something more?

Thank you for being so patient for me :) I've spend a lot of time on all of these issues but developing plugins for IntelliJ it's not that easy for newcomers :)

Have a nice day,
Mateusz

0
Comment actions Permalink

> I've tried to implement the lang.findUsagesProvider with language="JavaScript" but it seems that IntelliJ doesn't care about it.

did you try to put a breakpoint to the constructor? is it ever hit?

 

0
Comment actions Permalink

Yes, the constructor is hit during IDE startup. Other methods are never reached.

0
Comment actions Permalink

 

in your FindUsagesProvider, does

@Override
public boolean canFindUsagesFor(@NotNull PsiElement element) 

return true for the String identifier?

 

you may also need to implement your own WordsScanner and return your implementation:

@Nullable
@Override
public WordsScanner getWordsScanner() {
return WORDS_SCANNER;
}

0
Comment actions Permalink

I have a class SymbolFindUsagesProvider which implements all methods of FindUsagesProvider. I've set breakpoints on all methods and none of them hits when I press alt+F7 anywhere in a JavaScript file. Especially they are not hit when I press alt+F7 on string literal in the source code. Only a breakpoint at constructor is hit during IDE startup. 

Is this a desired behavior? Perhaps there is no way to define FindUsagesProvider for non-custom languages?

0
Comment actions Permalink

find usages is complex topic.

try to implement

 QueryExecutorBase<PsiReference, ReferencesSearch.SearchParameters>

(referencesSearch extension point)

 

see if its processQuery is called

0
Comment actions Permalink

Hi,

Thank you for the hint. I had to hack IntelliJ a little but finally I got it to work :) I will share a link to source code here once it's done.

0
Comment actions Permalink

> I got it to work

Well done. That's what matters. 

 

0

Please sign in to leave a comment.