Skip to content

Commit

Permalink
Merge pull request #125 from NipunaRanasinghe/pr/add-restart
Browse files Browse the repository at this point in the history
Add language server restart support
  • Loading branch information
NipunaRanasinghe committed Aug 20, 2019
2 parents ca5eed7 + 0d34468 commit a5e6276
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
38 changes: 35 additions & 3 deletions src/main/java/org/wso2/lsp4intellij/IntellijLanguageClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
Expand All @@ -38,17 +39,19 @@
import org.wso2.lsp4intellij.extensions.LSPExtensionManager;
import org.wso2.lsp4intellij.requests.Timeout;
import org.wso2.lsp4intellij.requests.Timeouts;
import org.wso2.lsp4intellij.utils.ApplicationUtils;
import org.wso2.lsp4intellij.utils.FileUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import static org.wso2.lsp4intellij.utils.ApplicationUtils.pool;

public class IntellijLanguageClient implements ApplicationComponent {

private static Logger LOG = Logger.getInstance(IntellijLanguageClient.class);
Expand Down Expand Up @@ -91,8 +94,10 @@ public static void addServerDefinition(LanguageServerDefinition definition, Proj
if (definition != null) {
if (project != null) {
processDefinition(definition, FileUtils.projectToUri(project));
restart(project);
} else {
processDefinition(definition, "");
restart();
}
LOG.info("Added definition for " + definition);
} else {
Expand Down Expand Up @@ -150,7 +155,7 @@ public static void editorOpened(Editor editor) {
}
String projectUri = FileUtils.projectToUri(project);
if (projectUri != null) {
ApplicationUtils.pool(() -> {
pool(() -> {
String ext = file.getExtension();
final String fileName = file.getName();
LOG.info("Opened " + fileName);
Expand Down Expand Up @@ -232,7 +237,7 @@ public static void editorClosed(Editor editor) {
return;
}

ApplicationUtils.pool(() -> {
pool(() -> {
LanguageServerWrapper serverWrapper = LanguageServerWrapper.forEditor(editor);
if (serverWrapper != null) {
LOG.info("Disconnecting " + FileUtils.editorToURIString(editor));
Expand All @@ -241,6 +246,33 @@ public static void editorClosed(Editor editor) {
});
}


/**
* This can be used to instantly apply a language server definition without restarting the IDE.
*/
public static void restart() {
Project[] openProjects = ProjectManager.getInstance().getOpenProjects();
for (Project project : openProjects) {
restart(project);
}
}

/**
* This can be used to instantly apply a project-specific language server definition without restarting the
* project/IDE.
*
* @param project The project instance which need to be restarted
*/
public static void restart(Project project) {
try {
List<Editor> allOpenedEditors = FileUtils.getAllOpenedEditors(project);
allOpenedEditors.forEach(IntellijLanguageClient::editorClosed);
allOpenedEditors.forEach(IntellijLanguageClient::editorOpened);
} catch (Exception e) {
LOG.warn(String.format("Refreshing project: %s is failed due to: ", project.getName()), e);
}
}

/**
* Returns current timeout values.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public Consumer<MouseEvent> getClickConsumer() {
actions.add(new ShowConnectedFiles());
}
actions.add(new ShowTimeouts());
if (wrapper.isResettable()) {
actions.add(new Reset());
if (wrapper.isRestartable()) {
actions.add(new Restart());
}

String title = "Server actions";
Expand Down Expand Up @@ -185,15 +185,15 @@ public void actionPerformed(AnActionEvent e) {
}
}

class Reset extends AnAction implements DumbAware {
class Restart extends AnAction implements DumbAware {

Reset() {
super("&Reset", "Reset the server so it can be started again.", null);
Restart() {
super("&Restart", "Restarts the language server.", null);
}

@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
wrapper.reset();
wrapper.restart();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public ServerCapabilities getServerCapabilities() {
}
} catch (TimeoutException e) {
notifyFailure(INIT);
String msg = String.format("%s \n not initialized after %ds \n Check settings",
String msg = String.format("%s \n is not initialized after %d seconds",
serverDefinition.toString(), getTimeout(INIT) / 1000);
LOG.warn(msg, e);
invokeLater(() -> {
Expand Down Expand Up @@ -673,20 +673,21 @@ private void connect(String uri) {
}

/**
* Is the langauge server in a state where it can be resettable. Normally language server is
* resettable if it has timedout or has a startup error.
* Is the language server in a state where it can be restartable. Normally language server is
* restartable if it has timeout or has a startup error.
*/
public boolean isResettable() {
public boolean isRestartable() {
return status == STOPPED && (alreadyShownTimeout || alreadyShownCrash);
}

/**
* Reset language server wrapper state so it can be started again if it was failed earlier.
*/
public void reset() {
if (isResettable()) {
public void restart() {
if (isRestartable()) {
alreadyShownCrash = false;
alreadyShownTimeout = false;
IntellijLanguageClient.restart(project);
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/wso2/lsp4intellij/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -65,6 +67,23 @@ public static String extFromPsiFile(PsiFile psiFile) {
return psiFile.getVirtualFile().getExtension();
}

public static List<Editor> getAllOpenedEditors(Project project) {
return computableReadAction(() -> {
List<Editor> editors = new ArrayList<>();
FileEditor[] allEditors = FileEditorManager.getInstance(project).getAllEditors();
for (FileEditor fEditor : allEditors) {
if (fEditor instanceof TextEditor) {
Editor editor = ((TextEditor) fEditor).getEditor();
if (editor.isDisposed() || !isEditorSupported(editor)) {
continue;
}
editors.add(editor);
}
}
return editors;
});
}

public static Editor editorFromPsiFile(PsiFile psiFile) {
return editorFromVirtualFile(psiFile.getVirtualFile(), psiFile.getProject());
}
Expand Down

0 comments on commit a5e6276

Please sign in to comment.