JavaScript API: Change Icon for JSLike file.

Is there a way to extend the JavaScript Language FileType? I would like to register files with the extension of .ds as JavaScript files with my plugin and provide a custom icon for those files. I would also like to potentially provide custom completions and such as well. Is this possible through the open api? I'm fine with providing only basic syntax with the community edition and the full JavaScript plugin with the ultimate edition, I'd just like to make some customizations if possible, specifcally the file icon.

0
17 comments

For Icons, use IconProvider (search the forum for more pointers).

It's no problem to register additional extensions for existing filetype, just register them via com.intellij.openapi.fileTypes.FileTypeFactory.

Regarding custom completions, please could you give more details on what you have in mind exactly?

0

I was able to register my Icon using the IconProvider using the following code.

 
public class DSIconProvider extends IconProvider {
    @Nullable
    @Override
    public
Icon getIcon(PsiElement element, int flags) {
        PsiFile containingFile = element.getContainingFile();
        if
(containingFile != null) {
            if (containingFile.getName() != null && containingFile.getName().contains(".ds")) {
                return DWIcons.DW_DS_ICON;
            
}
        }
        return null;
    
}
}


I know how to register a new FileType, but not how to register it as JavaScript. I can't find the class I need to add to my FileTypeFactory. Here is a custom filetype, but I would like to simply register DS as a JS FileType

 


public class DSLanguageTypeFactory extends FileTypeFactory {
    @Override
    public void createFileTypes
(@NotNull FileTypeConsumer consumer) {
        consumer.consume(DSFileType.INSTANCE, "ds");
    
}
}


The JS implementation is Rhino so this is server side code being executed through the script API. There is a Java api provided to work with the framework and I would like potentially add auto completion for this api. Maybe not even necessarily from a jar since this is for a SAAS platform and I do not have access to the server side code, but perhaps even just simple map lookups would help.

Thanks for the help!

0

Generic JS filetype is com.intellij.lang.javascript.JavaScriptFileType#INSTANCE (make sure to add jars from JavaScript plugin to your plugin SDK, not to your project).

0

OK that makes sense. Are the jars bundled with the community edition? Do you know what they are called and where I would find them? Thanks again!

0

JavaScript support is only available in Ultimate Edition.

$IDEA_HOME$/plugins/JavaScriptLanguage/lib

0

I was able to find all the jars and added them to my plugins lib folder. I then registered the ds file with the code below, but first the basic syntax highlighting does not work and second this seems to unregister JavaScript from the js extension. Is this the behavior I should expect?

public class DSLanguageTypeFactory extends FileTypeFactory {
    @Override
    public void createFileTypes(@NotNull FileTypeConsumer consumer) {
        consumer.consume(JavaScriptFileType.INSTANCE, "ds");
    } }
0

Certainly not. Please make sure you do not override your code with previous manual mapping of filetype to DS extension.

0

If I register the filetype in this way I get *.js files to open with an unregistered extension. If I remove this code I get the reverse for *.ds. I checked and there are no previous mappings. I created a branch with my code maybe you could take a peek?

https://github.com/nek4life/intellij-demandware/tree/feature/ds-js-support

My lib folder: Do I need all of these? Which version of Intellij should I use? currently using Intellij 14.

https://github.com/nek4life/intellij-demandware/tree/feature/ds-js-support/lib

My Plugin XML

https://github.com/nek4life/intellij-demandware/blob/feature/ds-js-support/resources/META-INF/plugin.xml#L39

And My FileTypeFactory class

https://github.com/nek4life/intellij-demandware/blob/feature/ds-js-support/src/com/demandware/studio/ds/DSLanguageTypeFactory.java

0

Your plugin.xml must contain a <dependency> on JavaScript plugin:

<depends>JavaScript</depends>
0

I added this line, but now I get an error saying that the "required plugin "JavaScript" not installed."

Does this mean that I cannot use this feature unless I'm using the ultimate edition? I'm currently running and debugging this with the Community Edition. My hope was that I could support Highlighting only with Community and Full JS support for Ultimate.

I really appreciate the time you're spending helping me with this. Thank you.

0

Exactly, you cannot provide this functionality outside of running Ultimate Edition.

You could extract this into optional plugin descriptor, which would only be loaded if required plugin is really available.
Just put this functionality into separate plugin.xml named javascript.xml and change your plugin.xml to something like

<depends config-file="javascript.xml" optional="true">JavaScript</depends>
0

Thank you. Is there any way to hook into the syntax highlighting only for community edition? Or would that entail writing my own language support? I have in the past registered ds using the new file type UI and added all the keywords there. In fact I've gone into the settings folder and found the XML after I set it all up so I could share it. Could I somehow create a file type using that XML for community edition through an API and then if the JS plugin is available load that instead? Hooking into the support already provided in community edition would be preferred of course though.

Thanks!

0

I've updated my code as you suggest and now it does not throw any errors in the community edition, however it does not map the files with a ds extension to the JavaScript language in the ultimate edition. I checked in the UI and in fileTypes.xml and ds is not mapped anywhere. I feel like I'm so close, but I'm not really sure how to proceed. :D

Link to updated code branch

https://github.com/nek4life/intellij-demandware/tree/feature/ds-js-support

0

Your javascript.xml has wrong declaration, it should be

<extensions defaultExtensionNs="com.intellij">


If this doesn't help. please check that your fileTypeConsumer is really called.

0

Alright I think that worked finally! Thank you so much. Just for future reference, how should I go about debugging in the Ultimate Edition, do I just create an SDK with the Ultimate Edition and debug from there? I could have sworn in the documentation it said you could only debug with the community edition and adding the sources. Thanks again!

0

Great! No idea about debugging though.. a simple System.println can help here, too :-)

0

Hey nothing like a good old System.out.println(); :D

Thanks again for the help!

0

Please sign in to leave a comment.