Repository navigation
Add a scijava-code-lsp ScriptDialect #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gselzer
wants to merge
32
commits into
main
Choose a base branch
from
lsp-completion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
c925dce
Provide Groovy code completion
gselzer 99398d5
Build the analyzed source from a ScriptDocument
gselzer 81a5b60
Serve Groovy as an in-process language server
gselzer 8204608
Move off scijava-code-api, now removed
gselzer 068d6ae
Type script bindings as last assigned
gselzer f0d5b67
Report problems in Groovy scripts
gselzer e8821b2
Ask the language servers through script sessions in tests
gselzer ce1e25c
Follow code-lsp's packages by purpose
gselzer bf678fc
Test signature order, not fit
gselzer 0a6b166
Map diagnostics with ScriptDocument directly
gselzer 8ba8333
Serve plain Groovy: sessions declare the parameters
gselzer 382ebe3
Follow ScriptDialect and ScriptDocument into code-lsp's core package
gselzer 851ec87
Gather completion into its package, and follow code-lsp
gselzer 4cadda8
Serve the script language by its name, as code-lsp now asks
gselzer ea48843
Offer only a Groovy dialect: the server moves to code-lsp-groovy
gselzer 833f35e
Test only the dialect: the server's tests move to code-lsp-groovy
gselzer 5df0a0e
Say what GroovyDialectTest's diagnostics test checks
gselzer b3fa1dd
Test the dialect itself, and need the server only to test
gselzer a70b939
Follow scijava-code-lsp's ScriptDialect.header
gselzer 956c4ad
Follow scijava-code-lsp: sessions are made with the service
gselzer 6f6dd51
Follow scijava-code-lsp: problems arrive through the listener
gselzer 23c9c3f
Follow scijava-code-lsp's ScriptDialect.header Variables
gselzer 3f7cc63
POM: say when scijava-common 2.101.0 needn't be pinned
gselzer 027600e
Implement ScriptDialect.outputLanguage, formerly languageId
gselzer 7313ffd
Follow scijava-code-lsp's rename to scijava-lsp
gselzer 5b1b8d2
Remove SNAPSHOT dependencies
gselzer cefd6ce
Add .idea folder to .gitignore
gselzer 1f1ca74
Bump JVM version to 11
gselzer b8da72a
Modularize scripting-groovy
gselzer 817dba2
Reorganize README
gselzer 9052a4d
Generalize POM version TEMP
gselzer 676a554
Surefire: Use property instead of plugin block
gselzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,4 +3,5 @@ | |
| /.idea/ | ||
| /.project | ||
| /.settings/ | ||
| /.idea/ | ||
| /target | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * #%L | ||
| * JSR-223-compliant Groovy scripting language plugin. | ||
| * %% | ||
| * Copyright (C) 2014 - 2026 SciJava developers. | ||
| * %% | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are met: | ||
| * | ||
| * 1. Redistributions of source code must retain the above copyright notice, | ||
| * this list of conditions and the following disclaimer. | ||
| * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE | ||
| * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| * POSSIBILITY OF SUCH DAMAGE. | ||
| * #L% | ||
| */ | ||
|
|
||
| module org.scijava.plugins.scripting.groovy { | ||
| exports org.scijava.plugins.scripting.groovy; | ||
| exports org.scijava.plugins.scripting.groovy.completion; | ||
|
Comment on lines
+31
to
+32
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually maybe neither of these |
||
|
|
||
| // NB: SciJava instantiates @Plugin classes reflectively. | ||
| opens org.scijava.plugins.scripting.groovy to org.scijava; | ||
| opens org.scijava.plugins.scripting.groovy.completion to org.scijava; | ||
|
|
||
| requires transitive java.scripting; | ||
| requires transitive org.apache.groovy; | ||
| requires transitive org.apache.groovy.jsr223; | ||
| requires transitive org.scijava; | ||
| requires transitive org.scijava.lsp; | ||
|
|
||
| provides javax.script.ScriptEngineFactory with | ||
| org.scijava.plugins.scripting.groovy.GroovyScriptLanguage; | ||
| } | ||
116 changes: 116 additions & 0 deletions
116
src/main/java/org/scijava/plugins/scripting/groovy/completion/GroovyDialect.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * #%L | ||
| * JSR-223-compliant Groovy scripting language plugin. | ||
| * %% | ||
| * Copyright (C) 2014 - 2026 SciJava developers. | ||
| * %% | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are met: | ||
| * | ||
| * 1. Redistributions of source code must retain the above copyright notice, | ||
| * this list of conditions and the following disclaimer. | ||
| * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE | ||
| * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| * POSSIBILITY OF SUCH DAMAGE. | ||
| * #L% | ||
| */ | ||
|
|
||
| package org.scijava.plugins.scripting.groovy.completion; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.TreeSet; | ||
|
|
||
| import groovyjarjarantlr4.v4.runtime.Vocabulary; | ||
|
|
||
| import org.apache.groovy.parser.antlr4.GroovyLexer; | ||
| import org.scijava.lsp.ScriptDialect; | ||
| import org.scijava.plugin.Plugin; | ||
| import org.scijava.plugins.scripting.groovy.GroovyScriptLanguage; | ||
| import org.scijava.script.ScriptLanguage; | ||
|
|
||
| /** | ||
| * Describes how to turn Groovy scripts into plain Groovy code, for | ||
| * {@code groovy} language servers (e.g. scijava-lsp-groovy's). | ||
| * | ||
| * @author Gabriel Selzer | ||
| */ | ||
| @Plugin(type = ScriptDialect.class, name = "Groovy") | ||
| public class GroovyDialect implements ScriptDialect { | ||
|
|
||
| /** Names Groovy reserves, which cannot be declared. */ | ||
| private static final Set<String> KEYWORDS = keywords(); | ||
|
|
||
| @Override | ||
| public boolean supports(final ScriptLanguage language) { | ||
| return language instanceof GroovyScriptLanguage; | ||
| } | ||
|
|
||
| @Override | ||
| public String outputLanguage() { | ||
| return "groovy"; | ||
| } | ||
|
|
||
| @Override | ||
| public Header header(final Map<String, Variable> variables) { | ||
| // Declare the types of each variable for type reasoning; Groovy gets | ||
| // the very Java objects, named in full, so nothing needs importing | ||
| final List<String> declarations = new ArrayList<>(); | ||
| variables.forEach((name, v) -> { | ||
| // NB: Names Groovy reserves cannot be declared. | ||
| if (!KEYWORDS.contains(name)) declarations.add(parameterType(v | ||
| .type()) + " " + name); | ||
| }); | ||
| // Combine into a Header | ||
| return new Header(String.join("; ", declarations), Collections | ||
| .emptySet()); | ||
| } | ||
|
|
||
| /** | ||
| * @param type the Java type (of a SciJava Script parameter) declared in the | ||
| * script | ||
| * @return the Groovy type of the object given to the script: the Java type | ||
| * itself, by its full name (or {@code Object} for anonymous and | ||
| * local classes, which have no name to write) | ||
| */ | ||
| static String parameterType(final Class<?> type) { | ||
| final String name = type.getCanonicalName(); | ||
| return name == null ? "Object" : name; | ||
| } | ||
|
|
||
| /** Groovy's keywords */ | ||
| private static Set<String> keywords() { | ||
| // NB: The lexer literals don't contain primitives - we add those ourselves | ||
| final Set<String> out = new TreeSet<>(Arrays.asList("true", "false", | ||
| "boolean", "byte", "char", "short", "int", "long", "float", "double")); | ||
| final Vocabulary v = GroovyLexer.VOCABULARY; | ||
| for (int i = 0; i <= v.getMaxTokenType(); i++) { | ||
| // NB: The vocabulary has every kind of token: keywords, operators and | ||
| // punctuation, which have fixed text (their literal name, quoted, e.g. | ||
| // "'class'"), plus identifiers, strings and numbers, which don't | ||
| // (null). We want just the literals that are words, i.e. keywords | ||
| // (with hyphens, as in "non-sealed"). | ||
| final String literal = v.getLiteralName(i); | ||
| if (literal == null) continue; | ||
| final String word = literal.substring(1, literal.length() - 1); | ||
| if (word.matches("[a-z][a-z-]*")) out.add(word); | ||
| } | ||
| return out; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.