Skip to content
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

Develop #4

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ intellij {
}

dependencies {
compile group: 'org.eclipse.lsp4j', name: 'org.eclipse.lsp4j', version: '0.15.0'
compile group: 'com.vladsch.flexmark', name: 'flexmark', version: '0.34.60'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
testCompile group: 'junit', name: 'junit', version: '4.13.2'
testCompile group: 'org.mockito', name: 'mockito-core', version: '3.9.0'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.9'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.9'
implementation group: 'org.eclipse.lsp4j', name: 'org.eclipse.lsp4j', version: '0.15.0'
implementation group: 'com.vladsch.flexmark', name: 'flexmark', version: '0.34.60'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.9.0'
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.9'
testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.9'
}

sourceSets {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public ProcessStreamConnectionProvider(@NotNull ProcessBuilder processBuilder) {

public void start() throws IOException {
if ((workingDir == null || commands == null || commands.isEmpty() || commands.contains(null)) && builder == null) {
throw new IOException("Unable to start language server: " + this.toString());
throw new IOException("Unable to start language server: " + this);
}
ProcessBuilder builder = createProcessBuilder();
LOG.info("Starting server process with commands " + commands + " and workingDir " + workingDir);
process = builder.start();
if (!process.isAlive()) {
throw new IOException("Unable to start language server: " + this.toString());
throw new IOException("Unable to start language server, process is not alive: " + this);
} else {
LOG.info("Server process started " + process);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.util.SmartList;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4j.DiagnosticTag;
Expand All @@ -39,7 +40,6 @@
import org.wso2.lsp4intellij.utils.FileUtils;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -75,10 +75,6 @@ public Object collectInformation(@NotNull PsiFile file, @NotNull Editor editor,
return null;
}

// If the diagnostics list is locked, we need to skip annotating the file.
if (!(eventManager.isDiagnosticSyncRequired() || eventManager.isCodeActionSyncRequired())) {
return null;
}
return RESULT;
} catch (Exception e) {
return null;
Expand All @@ -93,7 +89,10 @@ public Object doAnnotate(Object collectedInfo) {

@Override
public void apply(@NotNull PsiFile file, Object annotationResult, @NotNull AnnotationHolder holder) {

if (file.getVirtualFile() == null) {
LOG.warn("file == null || file.getVirtualFile() == null || file.getProject() == null: " + this);
return;
}
LanguageServerWrapper languageServerWrapper = LanguageServerWrapper.forVirtualFile(file.getVirtualFile(), file.getProject());
if (languageServerWrapper == null || languageServerWrapper.getStatus() != ServerStatus.INITIALIZED) {
return;
Expand All @@ -105,6 +104,13 @@ public void apply(@NotNull PsiFile file, Object annotationResult, @NotNull Annot
// TODO annotations are applied to a file / document not to an editor. so store them by file and not by editor..
EditorEventManager eventManager = EditorEventManagerBase.forUri(uri);

// If the diagnostics list is locked, we need to skip annotating the file.
if (!(eventManager.isDiagnosticSyncRequired() || eventManager.isCodeActionSyncRequired())) {
// return;
}

createAnnotations(holder, eventManager);
/*
if (eventManager.isCodeActionSyncRequired()) {
try {
updateAnnotations(holder, eventManager);
Expand All @@ -123,7 +129,13 @@ public void apply(@NotNull PsiFile file, Object annotationResult, @NotNull Annot
} catch (Throwable t) {
LOG.warn("Error occurred when updating LSP code actions.", t);
}
} else{
LOG.warn("no annotations");
}

*/
}else{
LOG.warn("no annotations");
}
}

Expand Down Expand Up @@ -151,9 +163,13 @@ protected Annotation createAnnotation(Editor editor, AnnotationHolder holder, Di
}
final TextRange range = new TextRange(start, end);

return holder.newAnnotation(lspToIntellijAnnotationsMap.get(diagnostic.getSeverity()), diagnostic.getMessage())
// Revert change from c05c7abe because createAnnotation() causes deprecation exception
holder.newAnnotation(lspToIntellijAnnotationsMap.get(diagnostic.getSeverity()), diagnostic.getMessage())
.range(range)
.createAnnotation();
.create();
SmartList<Annotation> asList = (SmartList<Annotation>) holder;

return asList.get(asList.size() - 1);
}

private void createAnnotations(AnnotationHolder holder, EditorEventManager eventManager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,20 @@
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.concurrency.AtomicFieldUpdater;
import com.intellij.util.keyFMap.KeyFMap;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Range;
import org.jetbrains.annotations.NotNull;
import org.wso2.lsp4intellij.utils.ApplicationUtils;
import org.wso2.lsp4intellij.editor.EditorEventManager;
import org.wso2.lsp4intellij.editor.EditorEventManagerBase;
import org.wso2.lsp4intellij.utils.DocumentUtils;
import org.wso2.lsp4intellij.utils.FileUtils;

import javax.annotation.Nullable;
import javax.swing.*;

import static org.wso2.lsp4intellij.utils.ApplicationUtils.invokeAfterPsiEvents;
import static org.wso2.lsp4intellij.utils.ApplicationUtils.writeAction;

/**
* A simple PsiElement for LSP
*/
Expand Down Expand Up @@ -720,13 +727,28 @@ public String getName() {
}

public void navigate(boolean requestFocus) {
Editor editor = FileUtils.editorFromPsiFile(getContainingFile());
if (editor == null) {
OpenFileDescriptor descriptor = new OpenFileDescriptor(getProject(), getContainingFile().getVirtualFile(),
getTextOffset());
ApplicationUtils.invokeLater(() -> ApplicationUtils
.writeAction(() -> FileEditorManager.getInstance(getProject()).openTextEditor(descriptor, false)));
}
writeAction(() -> {
Editor editor = FileUtils.editorFromPsiFile(getContainingFile());
if (editor == null) {
OpenFileDescriptor descriptor = new OpenFileDescriptor(getProject(), getContainingFile().getVirtualFile(),
getTextOffset());

editor = FileEditorManager.getInstance(getProject()).openTextEditor(descriptor, false);
}

Editor finalEditor = editor;
invokeAfterPsiEvents(() -> {
EditorEventManager manager = EditorEventManagerBase.forEditor(finalEditor);
if (manager != null) {
manager.gotoLocation(
new Location(
FileUtils.uriFromVirtualFile(getContainingFile().getVirtualFile()),
new Range(
DocumentUtils.offsetToLSPPos(finalEditor, this.start),
DocumentUtils.offsetToLSPPos(finalEditor, this.end))));
}
});
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
Expand Down Expand Up @@ -54,6 +55,7 @@

public class LSPServerStatusWidget implements StatusBarWidget {

private static final Logger LOG = Logger.getInstance(LSPServerStatusWidget.class);
private final Map<Timeouts, Pair<Integer, Integer>> timeouts = new HashMap<>();
private final Project project;
private final String projectName;
Expand Down Expand Up @@ -112,7 +114,11 @@ public void dispose() {
if (statusBar != null)
ApplicationUtils.invokeLater(() -> {
StatusBarWidgetFactory factory = ServiceManager.getService(StatusBarWidgetFactory.class);
factory.disposeWidget(this);
if (factory != null) {
factory.disposeWidget(this);
} else {
LOG.warn("factory is null in dispose(): " + this);
}
});
}
}
Expand Down Expand Up @@ -147,6 +153,10 @@ public Consumer<MouseEvent> getClickConsumer() {
JBPopupFactory.ActionSelectionAid mnemonics = JBPopupFactory.ActionSelectionAid.MNEMONICS;
Component component = t.getComponent();
List<AnAction> actions = new ArrayList<>();
if (LanguageServerWrapper.forProject(project) == null) {
LOG.warn("LanguageServerWrapper.forProject(project) is null: " + this);
return;
}
if (LanguageServerWrapper.forProject(project).getStatus() == ServerStatus.INITIALIZED) {
actions.add(new ShowConnectedFiles());
}
Expand Down Expand Up @@ -227,7 +237,7 @@ public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
public String getTooltipText() {
LanguageServerWrapper wrapper = LanguageServerWrapper.forProject(project);
if (wrapper == null) {
return "Language server, project " + projectName;
return "Language server wrapper is null for project " + projectName;
} else {
return "Language server for extension " + wrapper.getServerDefinition().ext + ", project " + projectName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.wso2.lsp4intellij.utils;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.NoAccessDuringPsiEvents;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Condition;
Expand All @@ -25,6 +26,7 @@

public class ApplicationUtils {

private static final Logger LOG = Logger.getInstance(ApplicationUtils.class);
private final static ExecutorService EXECUTOR_SERVICE;

static {
Expand All @@ -45,7 +47,11 @@ static public void invokeLater(Runnable runnable) {
}

static public void pool(Runnable runnable) {
EXECUTOR_SERVICE.submit(runnable);
try {
EXECUTOR_SERVICE.submit(runnable);
} catch(Exception e) {
LOG.warn(e + ": ApplicationUtils");
}
}

static public <T> T computableReadAction(Computable<T> computable) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/wso2/lsp4intellij/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ public static String documentToUri(Document document) {
return sanitizeURI(VFSToURI(FileDocumentManager.getInstance().getFile(document)));
}

@Nullable
public static String uriFromVirtualFile(@NotNull VirtualFile vf) {
return pathToUri(vf.getCanonicalPath());
}

/**
* Object representing the OS type (Windows or Unix)
*/
Expand Down