Git Product home page Git Product logo

jetbrains-plugin-sample's Introduction

Sample IntelliJ plugin using ANTLR grammar

This is a demonstration of ANTLRv4 library for IntelliJ plugins, which makes it easy to create plugins for IntelliJ-based IDEs based on an ANTLRv4 grammar.

Running the plugin for the first time

Make sure the Gradle plugin is installed in your IDE, go to File -> Open, select the build.gradle file and choose Open as Project.

If you already imported the project when it was not based on Gradle, then choose the option to delete the existing project and reimport it.

Once the IDE is done downloading dependencies and refreshing the project, you can use the Gradle tool window and use the following Tasks:

  • build > assemble to build the project
  • intellij > runIde to run the plugin in a sandboxed instance

Noteworthy things

Gradle build

The build is based on Gradle, and uses the gradle-intellij-plugin, which makes it easy to:

  • pull dependencies, especially the IntelliJ SDK and antlr4-intellij-adaptor
  • build and run tests in a CI environment on different versions of the SDK
  • generate lexers & parsers from your grammars, thanks to the ANTLR plugin for Gradle
  • publish plugins to the JetBrains Plugins Repository
  • configure the project for occasional contributors ๐Ÿ™‚

ANTLRPsiNode

PSI nodes defined in the plugin extend ANTLRPsiNode and IdentifierDefSubtree, which automatically makes them PsiNameIdentifierOwners.

Error highlighting

Errors are shown by SampleExternalAnnotator, which makes use of org.antlr.intellij.adaptor.xpath.XPath to detect references to unknown functions.

ParserDefinition

SampleParserDefinition uses several handy classes from the adaptor library:

  • PSIElementTypeFactory to generate IElementTypes from tokens and rules defined in your ANTLRv4 grammar
  • ANTLRLexerAdaptor to bind generated lexers to a com.intellij.lexer.Lexer
  • ANTLRParserAdaptor to bind generated parsers to a com.intellij.lang.PsiParser

Misc

WARNING. Turn on Dragon speech recognition for Mac and do a rename. GUI deadlocks. Every time. Turn off dragon. No problem ever. See JetBrains forum.

jetbrains-plugin-sample's People

Contributors

bjansen avatar kisioj avatar mathiasburger avatar parrt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jetbrains-plugin-sample's Issues

Missed issues in editor

A little bit strange, but the IDEA editor doesn't show any errors for the following code:

var b //must have assignment
func fun(){
    return //must return something
}

I have checked the same example in ANTLRv4 plugin and it shows 2 errors.

Lexer causes offset mismatch -> IllegalStateException; no file displayed

I'm trying to use my Prolog grammar to build a jetbrains prolog plugin. I appended a rule ERRCHAR: . ->channel(HIDDEN); and rewrote/threw out all the "sample language" specific stuff, so I can run the IDE with my plugin. Opening a prolog file doesn't work, however, I see in idea.log this backtrace (repeatedly, with different origins):

2018-03-07 22:36:34,339 [ 451765]  ERROR - oncurrency.BoundedTaskExecutor - Unexpected termination offset for lexer org.antlr.jetbrains.adaptor.lexer.ANTLRLexerAdaptor@5dc85b57 
java.lang.IllegalStateException: Unexpected termination offset for lexer org.antlr.jetbrains.adaptor.lexer.ANTLRLexerAdaptor@5dc85b57
	at com.intellij.openapi.editor.ex.util.LexerEditorHighlighter.doSetText(LexerEditorHighlighter.java:394)
	at com.intellij.openapi.editor.ex.util.LexerEditorHighlighter.setText(LexerEditorHighlighter.java:360)
	at com.intellij.openapi.fileEditor.impl.text.AsyncHighlighterUpdater.performInReadAction(AsyncHighlighterUpdater.java:59)
	at com.intellij.openapi.progress.util.ReadTask.lambda$runBackgroundProcess$0(ReadTask.java:66)
	at com.intellij.openapi.application.ReadAction.compute(ReadAction.java:47)
	at com.intellij.openapi.progress.util.ReadTask.runBackgroundProcess(ReadTask.java:66)

Debugging shows me that the exception stems here https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-ex/src/com/intellij/openapi/editor/ex/util/LexerEditorHighlighter.java#L394
because textLength (by the ANTLR lexer) is one bigger than mySegments.myEnds[last].

Has anybody else encountered a similar error? Is it a fault in my rewrite of the surrounding plugin files, or a bug somewhere else?
Would anybody be willing to look into the problem, if I publish the project so far?

Adding new custom keywords leads to errors

Hello,

Today I have been trying to use this plugin to create a new language for a JetBrains product.
I however can't extend the plugin with new keywords.

I keep getting errors such as:

image
image

While the ANTLR preview works just fine:
image

I have tried to add a block keywords (e.g. 'class') that works very similar to a func definition:

script
	:	vardef* define_class* function* statement* EOF
	;

define_class
    : 'class' ID block
    ;

// ...

CLASS : 'class' ;

And I have tried to copy the print() definition to log(), which also leads to errors in the IDE (but not in the preview):

image
image

	|	'print' '(' expr? ')'								# Print
	|	'log' '(' expr? ')'								 # Log

// ...

PRINT : 'print' ;
LOG : 'log' ;

When replacing print with log, it does work:

image


I have tried editing and copying Java files/classes and cases/rules to match my new keywords, but this did not help at all.
What am I missing/supposed to be doing in order to create new keywords?

Commit on antlr/jetbrains broke sample

This commit antlr/antlr4-intellij-adaptor@57db94a

Broke (at least) this source file: /jetbrains-plugin-sample/src/java/org/antlr/jetbrains/sample/psi/VardefSubtree.java.

The constructor signature was changed.

It's very easy to get around this:

$ cd adaptor
$ git checkout 0f4e20755f8ae7fe5bec1e8f52ccefab42f33a87

But I think this repo should still be fixed because it took me a while to figure out.

Leaf nodes should not implement INamedElement (bug fix for find usages)

IdentifierPSINode is a leaf node in your AST, and it implements PsiNamedElement.

public class IdentifierPSINode extends ANTLRPsiLeafNode implements PsiNamedElement {

It should not implement PsiNamedElement.

The Custom Language tutorial says

While the referencing element and the referenced element both may have a name, only the element which introduces the name (e.g., the definition int x = 42) needs to implement PsiNamedElement. The referencing element at the point of usage (e.g., the x in the expression x + 1) should not implement PsiNamedElement since it does not have a name.

I based my leaf node implementations on IdentifierPSINode, so I had my leaves implement PsiNamedElement. I just realized that this was causing a bug with "Find Usages". When searching for usages from a declaration, there were no results. Changing my leaves to not implement this interface fixed this problem.

Before I found this fix, I found the following workaround. I had my declaration be a reference to itself. In this case, "right-click -> Find Usages" works correctly, but "right-click -> Go To -> Declaration or Usages" does not. If there was only one usage, then the correct behavior would be to go to that usage. Instead, the behavior in this case is that the cursor moves to the beginning of the declaration (which is the current behavior for the current ANTLR plugin antlr/intellij-plugin-v4#580).

NoClassDefFoundError

Hi, I tried run this project, and the compile phase seams to be well, but when running the program, this error appear:

[   8276]  ERROR - i.util.KeyedExtensionCollector - org/antlr/v4/runtime/Parser 
java.lang.NoClassDefFoundError: org/antlr/v4/runtime/Parser

I have set the path variable as the antlr official website says:

export CLASSPATH=".:/usr/local/lib/antlr-4.5.3-complete.jar    :$CLASSPATH"

Could anyone help me solving the problem?

No string highlighting with \n

I have this code:

func f() {
    var s = "abc \n"
}

The problem is that "abc \n" is black (should be green because it is string). Problem appears if I use special symbols like \n.
When I set cursor before this string and then type backspace and space string is highlighted correctly.
However the parsing seems to be correctly in both cases.
Any ideas why this may happen?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.