Skip to content

Commit

Permalink
feat: Provide a language servers console like vscode (#838)
Browse files Browse the repository at this point in the history
Fixes #838

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed May 17, 2023
1 parent 6dcb518 commit 27703d6
Show file tree
Hide file tree
Showing 45 changed files with 2,128 additions and 337 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ task copyDeps(type: Copy) {

runIde {
systemProperties['com.redhat.devtools.intellij.telemetry.mode'] = 'disabled'
systemProperties['com.redhat.devtools.intellij.quarkus.trace'] = 'true'
}

runIdeForUiTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public IPsiUtils refine(Module module) {

@Override
public Module getModule(VirtualFile file) {
if (file != null) {
if (file != null && !project.isDisposed()) {
return ProjectFileIndex.getInstance(project).getModuleForFile(file, false);
}
return null;
Expand All @@ -92,7 +92,7 @@ public Module getModule(VirtualFile file) {
@Override
public Module getModule(String uri) throws IOException {
VirtualFile file = findFile(uri);
return file!=null?getModule(file):null;
return file != null ? getModule(file) : null;
}

@Override
Expand Down Expand Up @@ -171,15 +171,19 @@ public int toOffset(Document document, int line, int character) {
@Override
public int toOffset(PsiFile file, int line, int character) {
Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
return document!=null?toOffset(document, line, character):0;
return document != null ? toOffset(document, line, character) : 0;
}

@Override
public PsiFile resolveCompilationUnit(String uri) {
try {
VirtualFile file = findFile(uri);
if (file != null) {
return PsiManager.getInstance(getModule(file).getProject()).findFile(file);
Module module = getModule(file);
if (module == null) {
return null;
}
return PsiManager.getInstance(module.getProject()).findFile(file);
}
} catch (IOException e) {
LOGGER.error(e.getLocalizedMessage(), e);
Expand All @@ -202,7 +206,7 @@ public static ClasspathKind getClasspathKind(VirtualFile file, Module module) {
}

public static String getProjectURI(Module module) {
return module != null?module.getModuleFilePath():null;
return module != null ? module.getModuleFilePath() : null;
}

@Override
Expand All @@ -214,4 +218,4 @@ public String toUri(PsiFile typeRoot) {
public boolean isHiddenGeneratedElement(PsiElement element) {
return PsiUtils.isHiddenGeneratedElement(element);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.redhat.devtools.intellij.microprofile.lang;

import com.intellij.openapi.util.IconLoader;

import javax.swing.*;

public class MicroProfileIcons {

public static final Icon MicroProfile = IconLoader.findIcon("/microprofile_icon_rgb_16px_default.png", MicroProfileIcons.class);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.redhat.devtools.intellij.microprofile.lang;

import com.redhat.devtools.intellij.quarkus.lsp4ij.ServerIconProvider;

import javax.swing.*;

public class MicroProfileServerIconProvider implements ServerIconProvider {

@Override
public Icon getIcon() {
return MicroProfileIcons.MicroProfile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import javax.swing.Icon;

public class QuarkusIconProvider extends IconProvider {
private static final Icon QUARKUS_ICON = IconLoader.findIcon("/quarkus_icon_rgb_16px_default.png", QuarkusIconProvider.class);
public static final Icon QUARKUS_ICON = IconLoader.findIcon("/quarkus_icon_rgb_16px_default.png", QuarkusIconProvider.class);

@Nullable
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.redhat.devtools.intellij.quarkus.lang;

import com.redhat.devtools.intellij.microprofile.lang.MicroProfileIcons;
import com.redhat.devtools.intellij.quarkus.lsp4ij.ServerIconProvider;

import javax.swing.*;

public class QuarkusServerIconProvider implements ServerIconProvider {

@Override
public Icon getIcon() {
return QuarkusIconProvider.QUARKUS_ICON;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public QuarkusLanguageClient(Project project) {
QuarkusProjectService.getInstance(project);
}

@Override
public void dispose() {
connection.disconnect();
}

private void sendPropertiesChangeEvent(List<MicroProfilePropertiesScope> scope, Set<String> uris) {
MicroProfileLanguageServerAPI server = (MicroProfileLanguageServerAPI) getLanguageServer();
if (server != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,25 @@
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.intellij.quarkus.lsp4ij.lifecycle.LanguageServerLifecycleManager;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;

/**
* Track file opened / closed to start language servers / disconnect file from language servers.
*/
public class ConnectDocumentToLanguageServerSetupParticipant implements ProjectComponent, FileEditorManagerListener {

private static final Logger LOGGER = LoggerFactory.getLogger(ConnectDocumentToLanguageServerSetupParticipant.class);
public class ConnectDocumentToLanguageServerSetupParticipant implements ProjectManagerListener, FileEditorManagerListener {

private Project project;

public ConnectDocumentToLanguageServerSetupParticipant(Project project) {
this.project = project;
@Override
public void projectOpened(@NotNull Project project) {
project.getMessageBus().connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, this);
}

@Override
public void projectOpened() {
project.getMessageBus().connect(project).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, this);
public void projectClosing(@NotNull Project project) {
LanguageServerLifecycleManager.getInstance(project).dispose();
LanguageServiceAccessor.getInstance(project).shutdownAllDispatchers();
}

@Override
Expand All @@ -52,26 +47,4 @@ public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile f
}
}

@Override
public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
URI uri = LSPIJUtils.toUri(file);
if (uri != null) {
try {
// TODO: revisit this code, because it can restart language servers
// when a diagnostics is published after the file is closed and the project is closed
// See https://github.com/redhat-developer/intellij-quarkus/issues/840
// Remove the cached file wrapper if needed
LSPVirtualFileWrapper.dispose(file);
// Disconnect the given file from all language servers
LanguageServiceAccessor.getInstance(source.getProject())
.getLSWrappers(file, capabilities -> true)
.forEach(
wrapper -> wrapper.disconnect(uri)
);
} catch (Exception e) {
LOGGER.warn("Error while disconnecting the file '" + uri + "' from all language servers", e);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ private void sendDidChangeEvents() {
DidChangeTextDocumentParams changeParamsToSend = new DidChangeTextDocumentParams(new VersionedTextDocumentIdentifier(), events);
changeParamsToSend.getTextDocument().setUri(fileUri.toString());
changeParamsToSend.getTextDocument().setVersion(++version);
languageServerWrapper.getInitializedServer()
.thenAcceptAsync(ls -> ls.getTextDocumentService().didChange(changeParamsToSend));
languageServerWrapper.sendNotification(ls -> ls.getTextDocumentService().didChange(changeParamsToSend));
}

@Override
Expand Down Expand Up @@ -181,7 +180,7 @@ public void documentClosed() {
if (languageServerWrapper.isActive()) {
TextDocumentIdentifier identifier = new TextDocumentIdentifier(fileUri.toString());
DidCloseTextDocumentParams params = new DidCloseTextDocumentParams(identifier);
languageServerWrapper.getInitializedServer().thenAcceptAsync(ls -> ls.getTextDocumentService().didClose(params));
languageServerWrapper.sendNotification(ls -> ls.getTextDocumentService().didClose(params));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static <T extends TextDocumentPositionParams> T toTextDocumentPositionPa
param.setPosition(start);
TextDocumentIdentifier id = new TextDocumentIdentifier();
if (uri != null) {
id.setUri(uri.toString());
id.setUri(uri.toASCIIString());
}
param.setTextDocument(id);
return param;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,7 @@ public CompletableFuture<List<WorkspaceFolder>> workspaceFolders() {
return CompletableFuture.completedFuture(res);
}

public void dispose() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.redhat.devtools.intellij.quarkus.lsp4ij;

import javax.swing.*;

public class LanguageServerIconProviderDefinition {

private final ServerIconProviderExtensionPointBean extension;

public LanguageServerIconProviderDefinition(ServerIconProviderExtensionPointBean extension) {
this.extension = extension;
}

public Icon getIcon() {
return extension.getInstance().getIcon();
}
}
Loading

0 comments on commit 27703d6

Please sign in to comment.