chore: Code Cleanup

This commit is contained in:
FalsePattern 2023-07-31 15:15:09 +02:00 committed by FalsePattern
parent b3c13d287a
commit e2d2259b73
Signed by: falsepattern
GPG key ID: FDF7126A9E124447
8 changed files with 93 additions and 62 deletions

View file

@ -18,7 +18,6 @@ package com.falsepattern.zigbrains;
import com.falsepattern.zigbrains.lsp.ZLSEditorEventManager; import com.falsepattern.zigbrains.lsp.ZLSEditorEventManager;
import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.editor.markup.HighlighterLayer; import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.HighlighterTargetArea; import com.intellij.openapi.editor.markup.HighlighterTargetArea;
import com.intellij.openapi.editor.markup.RangeHighlighter; import com.intellij.openapi.editor.markup.RangeHighlighter;
@ -26,7 +25,6 @@ import com.intellij.openapi.util.Key;
import org.wso2.lsp4intellij.editor.EditorEventManager; import org.wso2.lsp4intellij.editor.EditorEventManager;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -35,6 +33,7 @@ import java.util.stream.Stream;
public class HighlightingUtil { public class HighlightingUtil {
private static final Key<Integer> HL_HASH = Key.create("HIGHLIGHTING_HASH"); private static final Key<Integer> HL_HASH = Key.create("HIGHLIGHTING_HASH");
public static void refreshHighlighting(EditorEventManager eem) { public static void refreshHighlighting(EditorEventManager eem) {
var app = ApplicationManager.getApplication(); var app = ApplicationManager.getApplication();
if (!(eem instanceof ZLSEditorEventManager manager)) { if (!(eem instanceof ZLSEditorEventManager manager)) {
@ -84,12 +83,13 @@ public class HighlightingUtil {
} }
} }
} }
for (var edit: range.add()) { for (var edit : range.add()) {
var end = edit.end(); var end = edit.end();
if (end > documentLength - 1) { if (end > documentLength - 1) {
end = documentLength - 1; end = documentLength - 1;
} }
markup.addRangeHighlighter(edit.color(), edit.start(), end, HighlighterLayer.SYNTAX, HighlighterTargetArea.EXACT_RANGE); markup.addRangeHighlighter(edit.color(), edit.start(), end, HighlighterLayer.SYNTAX,
HighlighterTargetArea.EXACT_RANGE);
} }
} }
}); });

View file

@ -25,7 +25,20 @@ import java.util.Locale;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.*; import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.CLASS_NAME;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.CLASS_REFERENCE;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.FUNCTION_DECLARATION;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.GLOBAL_VARIABLE;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.KEYWORD;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.LABEL;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.LINE_COMMENT;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.LOCAL_VARIABLE;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.NUMBER;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.OPERATION_SIGN;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.PARAMETER;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.STATIC_FIELD;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.STATIC_METHOD;
import static com.intellij.openapi.editor.DefaultLanguageHighlighterColors.STRING;
public enum ZigAttributes { public enum ZigAttributes {
Type(CLASS_NAME), Type(CLASS_NAME),

View file

@ -31,8 +31,7 @@ import java.util.Map;
public class ZigColorSettingsPage implements ColorSettingsPage { public class ZigColorSettingsPage implements ColorSettingsPage {
private static final AttributesDescriptor[] DESCRIPTORS = private static final AttributesDescriptor[] DESCRIPTORS =
new AttributesDescriptor[]{ new AttributesDescriptor[]{new AttributesDescriptor("Builtin", ZigAttributes.Builtin.KEY),
new AttributesDescriptor("Builtin", ZigAttributes.Builtin.KEY),
new AttributesDescriptor("Comment", ZigAttributes.Comment.KEY), new AttributesDescriptor("Comment", ZigAttributes.Comment.KEY),
new AttributesDescriptor("Enum", ZigAttributes.Enum.KEY), new AttributesDescriptor("Enum", ZigAttributes.Enum.KEY),
new AttributesDescriptor("Enum member", ZigAttributes.EnumMember.KEY), new AttributesDescriptor("Enum member", ZigAttributes.EnumMember.KEY),
@ -49,8 +48,7 @@ public class ZigColorSettingsPage implements ColorSettingsPage {
new AttributesDescriptor("String", ZigAttributes.String.KEY), new AttributesDescriptor("String", ZigAttributes.String.KEY),
new AttributesDescriptor("Struct", ZigAttributes.Struct.KEY), new AttributesDescriptor("Struct", ZigAttributes.Struct.KEY),
new AttributesDescriptor("Type", ZigAttributes.Type.KEY), new AttributesDescriptor("Type", ZigAttributes.Type.KEY),
new AttributesDescriptor("Variable", ZigAttributes.Variable.KEY), new AttributesDescriptor("Variable", ZigAttributes.Variable.KEY)};
};
@Nullable @Nullable
@Override @Override

View file

@ -48,6 +48,7 @@ import static org.wso2.lsp4intellij.requests.Timeout.getTimeout;
public class ZLSEditorEventManager extends EditorEventManager { public class ZLSEditorEventManager extends EditorEventManager {
private static String previousResultID = null; private static String previousResultID = null;
public ZLSEditorEventManager(Editor editor, DocumentListener documentListener, EditorMouseListener mouseListener, EditorMouseMotionListener mouseMotionListener, LSPCaretListenerImpl caretListener, RequestManager requestmanager, ServerOptions serverOptions, LanguageServerWrapper wrapper) { public ZLSEditorEventManager(Editor editor, DocumentListener documentListener, EditorMouseListener mouseListener, EditorMouseMotionListener mouseMotionListener, LSPCaretListenerImpl caretListener, RequestManager requestmanager, ServerOptions serverOptions, LanguageServerWrapper wrapper) {
super(editor, documentListener, mouseListener, mouseMotionListener, caretListener, requestmanager, super(editor, documentListener, mouseListener, mouseMotionListener, caretListener, requestmanager,
serverOptions, wrapper); serverOptions, wrapper);

View file

@ -35,6 +35,7 @@ import java.util.concurrent.locks.ReentrantLock;
public class ZLSStartupActivity implements StartupActivity { public class ZLSStartupActivity implements StartupActivity {
private static final ReentrantLock lock = new ReentrantLock(); private static final ReentrantLock lock = new ReentrantLock();
public static void initZLS() { public static void initZLS() {
ApplicationManager.getApplication().executeOnPooledThread(() -> { ApplicationManager.getApplication().executeOnPooledThread(() -> {
lock.lock(); lock.lock();
@ -48,8 +49,8 @@ public class ZLSStartupActivity implements StartupActivity {
boolean configOK = true; boolean configOK = true;
if (!"".equals(configPath) && !validatePath("ZLS Config", configPath, false)) { if (!"".equals(configPath) && !validatePath("ZLS Config", configPath, false)) {
configOK = false; configOK = false;
Notifications.Bus.notify( Notifications.Bus.notify(new Notification("ZigBrains.Nag", "Using default config path.",
new Notification("ZigBrains.Nag", "Using default config path.", NotificationType.INFORMATION)); NotificationType.INFORMATION));
} }
if ("".equals(configPath)) { if ("".equals(configPath)) {
configOK = false; configOK = false;
@ -87,7 +88,8 @@ public class ZLSStartupActivity implements StartupActivity {
} }
} }
} }
IntellijLanguageClient.addServerDefinition(new RawCommandServerDefinition("zig", cmd.toArray(String[]::new))); IntellijLanguageClient.addServerDefinition(
new RawCommandServerDefinition("zig", cmd.toArray(String[]::new)));
} finally { } finally {
lock.unlock(); lock.unlock();
} }
@ -106,15 +108,16 @@ public class ZLSStartupActivity implements StartupActivity {
} }
if (!Files.exists(path)) { if (!Files.exists(path)) {
Notifications.Bus.notify(new Notification("ZigBrains.Nag", "No " + name, Notifications.Bus.notify(new Notification("ZigBrains.Nag", "No " + name,
"The " + name + " at \"" + pathTxt + "The " + name + " at \"" + pathTxt + "\" doesn't exist!",
"\" doesn't exist!", NotificationType.ERROR)); NotificationType.ERROR));
return false; return false;
} }
if (Files.isDirectory(path) != dir) { if (Files.isDirectory(path) != dir) {
Notifications.Bus.notify(new Notification("ZigBrains.Nag", "No " + name, Notifications.Bus.notify(new Notification("ZigBrains.Nag", "No " + name,
"The " + name + " at \"" + pathTxt + "The " + name + " at \"" + pathTxt + "\" is a " +
"\" is a " + (Files.isDirectory(path) ? "directory": "file") + (Files.isDirectory(path) ? "directory" : "file") +
" , expected a " + (dir ? "directory" : "file"), NotificationType.ERROR)); " , expected a " + (dir ? "directory" : "file"),
NotificationType.ERROR));
return false; return false;
} }
return true; return true;

View file

@ -41,13 +41,17 @@ public class AppSettingsComponent {
.addComponent(new JBLabel("Regular settings")) .addComponent(new JBLabel("Regular settings"))
.addVerticalGap(10) .addVerticalGap(10)
.addLabeledComponent(new JBLabel("ZLS path: "), zlsPathText, 1, false) .addLabeledComponent(new JBLabel("ZLS path: "), zlsPathText, 1, false)
.addLabeledComponent(new JBLabel("ZLS config path (leave empty to use default): "), zlsConfigPathText, 1, false) .addLabeledComponent(new JBLabel("ZLS config path (leave empty to use default): "),
zlsConfigPathText, 1, false)
.addLabeledComponent(new JBLabel("Increase timeouts"), increaseTimeouts, 1, false) .addLabeledComponent(new JBLabel("Increase timeouts"), increaseTimeouts, 1, false)
.addSeparator() .addSeparator()
.addComponent(new JBLabel("Developer settings (only usable when the IDE was launched with the runIDE gradle task in ZigBrains!)")) .addComponent(new JBLabel("Developer settings" +
" (only usable when the IDE was launched with " +
"the runIDE gradle task in ZigBrains!)"))
.addVerticalGap(10) .addVerticalGap(10)
.addLabeledComponent(new JBLabel("ZLS debug log: "), debugCheckBox, 1, false) .addLabeledComponent(new JBLabel("ZLS debug log: "), debugCheckBox, 1, false)
.addLabeledComponent(new JBLabel("ZLS message trace: "), messageTraceCheckBox, 1, false) .addLabeledComponent(new JBLabel("ZLS message trace: "), messageTraceCheckBox, 1,
false)
.addComponentFillVertically(new JPanel(), 0) .addComponentFillVertically(new JPanel(), 0)
.getPanel(); .getPanel();
} }

View file

@ -16,8 +16,8 @@
package com.falsepattern.zigbrains.util; package com.falsepattern.zigbrains.util;
import com.falsepattern.zigbrains.ide.ZigAttributes;
import com.falsepattern.zigbrains.ide.SemaRange; import com.falsepattern.zigbrains.ide.SemaRange;
import com.falsepattern.zigbrains.ide.ZigAttributes;
import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition; import com.intellij.openapi.editor.LogicalPosition;
@ -30,25 +30,12 @@ import java.util.List;
import java.util.Set; import java.util.Set;
public class TokenDecoder { public class TokenDecoder {
private record Token(int line, int start, int length, int type, int modifiers) {
public static Token from(Token prevToken, List<Integer> data, int index) {
int line = data.get(index);
int start = data.get(index + 1);
if (prevToken != null) {
if (line == 0) {
start += prevToken.start();
}
line += prevToken.line();
}
return new Token(line, start, data.get(index + 2), data.get(index + 3), data.get(index + 4));
}
}
public static List<SemaRange> decodePayload(int baseOffset, Editor editor, SemanticTokensLegend legend, List<Integer> responseData) { public static List<SemaRange> decodePayload(int baseOffset, Editor editor, SemanticTokensLegend legend, List<Integer> responseData) {
var result = new ArrayList<SemaRange>(); var result = new ArrayList<SemaRange>();
var application = ApplicationManager.getApplication(); var application = ApplicationManager.getApplication();
int dataSize = responseData.size(); int dataSize = responseData.size();
var startPos = application.runReadAction((Computable<LogicalPosition>)() -> editor.offsetToLogicalPosition(baseOffset)); var startPos = application.runReadAction(
(Computable<LogicalPosition>) () -> editor.offsetToLogicalPosition(baseOffset));
Token prevToken = new Token(startPos.line, startPos.column, 0, 0, 0); Token prevToken = new Token(startPos.line, startPos.column, 0, 0, 0);
var types = legend.getTokenTypes(); var types = legend.getTokenTypes();
var modifiers = legend.getTokenModifiers(); var modifiers = legend.getTokenModifiers();
@ -76,4 +63,18 @@ public class TokenDecoder {
return result; return result;
} }
private record Token(int line, int start, int length, int type, int modifiers) {
public static Token from(Token prevToken, List<Integer> data, int index) {
int line = data.get(index);
int start = data.get(index + 1);
if (prevToken != null) {
if (line == 0) {
start += prevToken.start();
}
line += prevToken.line();
}
return new Token(line, start, data.get(index + 2), data.get(index + 3), data.get(index + 4));
}
}
} }

View file

@ -17,7 +17,8 @@
<fileDocumentManagerListener implementation="org.wso2.lsp4intellij.listeners.LSPFileDocumentManagerListener"/> <fileDocumentManagerListener implementation="org.wso2.lsp4intellij.listeners.LSPFileDocumentManagerListener"/>
<!-- for displaying notifications by lsp --> <!-- for displaying notifications by lsp -->
<notificationGroup id="lsp" displayType="STICKY_BALLOON"/> <notificationGroup id="lsp"
displayType="STICKY_BALLOON"/>
<!-- for displaying the statusbar icon --> <!-- for displaying the statusbar icon -->
<statusBarWidgetFactory implementation="org.wso2.lsp4intellij.statusbar.LSPServerStatusWidgetFactory" <statusBarWidgetFactory implementation="org.wso2.lsp4intellij.statusbar.LSPServerStatusWidgetFactory"
@ -26,13 +27,15 @@
<!-- needed for completion --> <!-- needed for completion -->
<completion.contributor implementationClass="org.wso2.lsp4intellij.contributors.LSPCompletionContributor" <completion.contributor implementationClass="org.wso2.lsp4intellij.contributors.LSPCompletionContributor"
id="org.wso2.lsp4intellij.contributors.LSPCompletionContributor" language="any"/> id="org.wso2.lsp4intellij.contributors.LSPCompletionContributor"
language="any"/>
<!-- needed for completion as well as signature help --> <!-- needed for completion as well as signature help -->
<typedHandler implementation="org.wso2.lsp4intellij.listeners.LSPTypedHandler" <typedHandler implementation="org.wso2.lsp4intellij.listeners.LSPTypedHandler"
id="LSPTypedHandler"/> id="LSPTypedHandler"/>
<!-- needed for code diagnostics --> <!-- needed for code diagnostics -->
<externalAnnotator id="LSPAnnotator" language="Zig" <externalAnnotator id="LSPAnnotator"
language="Zig"
implementationClass="org.wso2.lsp4intellij.contributors.annotator.LSPAnnotator"/> implementationClass="org.wso2.lsp4intellij.contributors.annotator.LSPAnnotator"/>
<!-- needed for Workspace Symbols --> <!-- needed for Workspace Symbols -->
@ -40,14 +43,18 @@
id="LSPSymbolContributor"/> id="LSPSymbolContributor"/>
<!-- needed for renaming --> <!-- needed for renaming -->
<renameHandler implementation="org.wso2.lsp4intellij.contributors.rename.LSPRenameHandler" id="LSPRenameHandler" <renameHandler implementation="org.wso2.lsp4intellij.contributors.rename.LSPRenameHandler"
id="LSPRenameHandler"
order="first"/> order="first"/>
<renamePsiElementProcessor implementation="org.wso2.lsp4intellij.contributors.rename.LSPRenameProcessor" <renamePsiElementProcessor implementation="org.wso2.lsp4intellij.contributors.rename.LSPRenameProcessor"
id="LSPRenameProcessor" order="first"/> id="LSPRenameProcessor"
order="first"/>
<!-- needed for folding support --> <!-- needed for folding support -->
<lang.foldingBuilder language="Zig" <lang.foldingBuilder language="Zig"
implementationClass="org.wso2.lsp4intellij.contributors.LSPFoldingRangeProvider" id="LSPFoldingRangeProvider" order="first" /> implementationClass="org.wso2.lsp4intellij.contributors.LSPFoldingRangeProvider"
id="LSPFoldingRangeProvider"
order="first"/>
<!-- endregion LSP4IntelliJ --> <!-- endregion LSP4IntelliJ -->
@ -65,7 +72,8 @@
displayName="Zig"/> displayName="Zig"/>
<postStartupActivity implementation="com.falsepattern.zigbrains.lsp.ZLSStartupActivity"/> <postStartupActivity implementation="com.falsepattern.zigbrains.lsp.ZLSStartupActivity"/>
<notificationGroup displayType="BALLOON" id="ZigBrains.Nag"/> <notificationGroup displayType="BALLOON"
id="ZigBrains.Nag"/>
</extensions> </extensions>
@ -73,12 +81,15 @@
<!-- region LSP4IntelliJ --> <!-- region LSP4IntelliJ -->
<!-- needed for hover --> <!-- needed for hover -->
<action id="org.intellij.sdk.action.QuickDocAction" class="org.wso2.lsp4intellij.actions.LSPQuickDocAction"> <action id="org.intellij.sdk.action.QuickDocAction"
class="org.wso2.lsp4intellij.actions.LSPQuickDocAction">
</action> </action>
<!-- needed for find references --> <!-- needed for find references -->
<action class="org.wso2.lsp4intellij.actions.LSPReferencesAction" id="LSPFindUsages"> <action class="org.wso2.lsp4intellij.actions.LSPReferencesAction"
<keyboard-shortcut first-keystroke="shift alt F7" keymap="$default"/> id="LSPFindUsages">
<keyboard-shortcut first-keystroke="shift alt F7"
keymap="$default"/>
</action> </action>
<!-- endregion LSP4IntelliJ --> <!-- endregion LSP4IntelliJ -->