Skip to content

Commit

Permalink
LSP: Format Document and Format Selection actions implemented. (apach…
Browse files Browse the repository at this point in the history
  • Loading branch information
dbalek authored May 20, 2022
1 parent de59faf commit bb9fd66
Show file tree
Hide file tree
Showing 14 changed files with 1,465 additions and 112 deletions.
14 changes: 14 additions & 0 deletions groovy/groovy.editor/apichanges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ is the proper place.
<!-- ACTUAL CHANGES BEGIN HERE: -->

<changes>
<change id="LexUtilities.LineDocument.methods">
<api name="groovy-parsing"/>
<summary>Variants of the exisitng methods taking LineDocument as argument added.</summary>
<version major="1" minor="85"/>
<date day="18" month="5" year="2022"/>
<author login="dbalek"/>
<compatibility addition="yes" binary="compatible" semantic="compatible" />
<description>
<p>
Variants of the exisitng methods taking LineDocument as argument added.
</p>
</description>
<class package="org.netbeans.modules.groovy.editor.api.lexer" name="LexUtilities"/>
</change>
<change id="ASTPath.outer">
<api name="groovy-parsing"/>
<summary>Alternative construction for path more suitable for expressions, API to resolve types</summary>
Expand Down
2 changes: 1 addition & 1 deletion groovy/groovy.editor/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ AutoUpdate-Show-In-Client: false
OpenIDE-Module: org.netbeans.modules.groovy.editor/3
OpenIDE-Module-Layer: org/netbeans/modules/groovy/editor/resources/layer.xml
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/groovy/editor/Bundle.properties
OpenIDE-Module-Specification-Version: 1.84
OpenIDE-Module-Specification-Version: 1.85
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.editor.document.LineDocument;
import org.netbeans.api.lexer.Token;
import org.netbeans.api.lexer.TokenHierarchy;
import org.netbeans.api.lexer.TokenId;
Expand Down Expand Up @@ -163,12 +164,16 @@ public static OffsetRange getLexerOffsets(GroovyParserResult info, OffsetRange a
/** Find the Groovy token sequence (in case it's embedded in something else at the top level. */
@SuppressWarnings("unchecked")
public static TokenSequence<GroovyTokenId> getGroovyTokenSequence(Document doc, int offset) {
final BaseDocument baseDocument = (BaseDocument) doc;
final BaseDocument baseDocument = doc instanceof BaseDocument ? (BaseDocument) doc : null;
try {
baseDocument.readLock();
if (baseDocument != null) {
baseDocument.readLock();
}
return getGroovyTokenSequence(TokenHierarchy.get(doc), offset);
} finally {
baseDocument.readUnlock();
if (baseDocument != null) {
baseDocument.readUnlock();
}
}
}

Expand Down Expand Up @@ -236,6 +241,10 @@ public static TokenSequence<GroovyTokenId> getPositionedSequence(BaseDocument do
return getPositionedSequence(doc, offset, true);
}

public static TokenSequence<GroovyTokenId> getPositionedSequence(LineDocument doc, int offset) {
return getPositionedSequence(doc, offset, true);
}

public static TokenSequence<GroovyTokenId> getPositionedSequence(BaseDocument doc, int offset, boolean lookBack) {
TokenSequence<GroovyTokenId> ts = getGroovyTokenSequence(doc, offset);

Expand Down Expand Up @@ -264,6 +273,34 @@ public static TokenSequence<GroovyTokenId> getPositionedSequence(BaseDocument do
return null;
}

public static TokenSequence<GroovyTokenId> getPositionedSequence(LineDocument doc, int offset, boolean lookBack) {
TokenSequence<GroovyTokenId> ts = getGroovyTokenSequence(doc, offset);

if (ts != null) {
try {
ts.move(offset);
} catch (AssertionError e) {
DataObject dobj = (DataObject) doc.getProperty(Document.StreamDescriptionProperty);

if (dobj != null) {
Exceptions.attachMessage(e, FileUtil.getFileDisplayName(dobj.getPrimaryFile()));
}

throw e;
}

if (!lookBack && !ts.moveNext()) {
return null;
} else if (lookBack && !ts.moveNext() && !ts.movePrevious()) {
return null;
}

return ts;
}

return null;
}

public static Token<GroovyTokenId> getToken(BaseDocument doc, int offset) {
TokenSequence<GroovyTokenId> ts = getGroovyTokenSequence(doc, offset);

Expand Down Expand Up @@ -292,6 +329,34 @@ public static Token<GroovyTokenId> getToken(BaseDocument doc, int offset) {
return null;
}

public static Token<GroovyTokenId> getToken(LineDocument doc, int offset) {
TokenSequence<GroovyTokenId> ts = getGroovyTokenSequence(doc, offset);

if (ts != null) {
try {
ts.move(offset);
} catch (AssertionError e) {
DataObject dobj = (DataObject) doc.getProperty(Document.StreamDescriptionProperty);

if (dobj != null) {
Exceptions.attachMessage(e, FileUtil.getFileDisplayName(dobj.getPrimaryFile()));
}

throw e;
}

if (!ts.moveNext() && !ts.movePrevious()) {
return null;
}

Token<GroovyTokenId> token = ts.token();

return token;
}

return null;
}

public static char getTokenChar(BaseDocument doc, int offset) {
Token<GroovyTokenId> token = getToken(doc, offset);

Expand Down Expand Up @@ -446,6 +511,15 @@ public static boolean isBeginToken(TokenId id, BaseDocument doc, int offset) {
return END_PAIRS.contains(id);
}

/**
* Return true iff the given token is a token that should be matched
* with a corresponding "end" token, such as "begin", "def", "module",
* etc.
*/
public static boolean isBeginToken(TokenId id, LineDocument doc, int offset) {
return END_PAIRS.contains(id);
}

/**
* Return true iff the given token is a token that should be matched
* with a corresponding "end" token, such as "begin", "def", "module",
Expand All @@ -455,6 +529,15 @@ public static boolean isBeginToken(TokenId id, BaseDocument doc, TokenSequence<G
return END_PAIRS.contains(id);
}

/**
* Return true iff the given token is a token that should be matched
* with a corresponding "end" token, such as "begin", "def", "module",
* etc.
*/
public static boolean isBeginToken(TokenId id, LineDocument doc, TokenSequence<GroovyTokenId> ts) {
return END_PAIRS.contains(id);
}

/**
* Return true iff the given token is a token that indents its content,
* such as the various begin tokens as well as "else", "when", etc.
Expand Down
Loading

0 comments on commit bb9fd66

Please sign in to comment.