org.eclipse.lsp4j.TextDocumentSyncOptions Java Examples

The following examples show how to use org.eclipse.lsp4j.TextDocumentSyncOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LanguageServerWrapper.java    From lsp4intellij with Apache License 2.0 5 votes vote down vote up
/**
 * @return if the server supports willSaveWaitUntil
 */
public boolean isWillSaveWaitUntil() {
    return Optional.ofNullable(getServerCapabilities())
      .map(ServerCapabilities::getTextDocumentSync)
      .map(Either::getRight)
      .map(TextDocumentSyncOptions::getWillSaveWaitUntil)
      .orElse(false);
}
 
Example #2
Source File: DocumentContentSynchronizer.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
public void documentSaved(long timestamp) {
    this.modificationStamp = timestamp;
    ServerCapabilities serverCapabilities = languageServerWrapper.getServerCapabilities();
    if(serverCapabilities != null ) {
        Either<TextDocumentSyncKind, TextDocumentSyncOptions> textDocumentSync = serverCapabilities.getTextDocumentSync();
        if(textDocumentSync.isRight() && textDocumentSync.getRight().getSave() == null) {
            return;
        }
    }
    TextDocumentIdentifier identifier = new TextDocumentIdentifier(fileUri.toString());
    DidSaveTextDocumentParams params = new DidSaveTextDocumentParams(identifier, document.getText());
    languageServerWrapper.getInitializedServer().thenAcceptAsync(ls -> ls.getTextDocumentService().didSave(params));
}
 
Example #3
Source File: SyntaxInitHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void registerCapabilities(InitializeResult initializeResult) {
	ServerCapabilities capabilities = new ServerCapabilities();
	if (!preferenceManager.getClientPreferences().isClientDocumentSymbolProviderRegistered() && !preferenceManager.getClientPreferences().isDocumentSymbolDynamicRegistered()) {
		capabilities.setDocumentSymbolProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isDefinitionDynamicRegistered()) {
		capabilities.setDefinitionProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isTypeDefinitionDynamicRegistered()) {
		capabilities.setTypeDefinitionProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isFoldgingRangeDynamicRegistered()) {
		capabilities.setFoldingRangeProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isSelectionRangeDynamicRegistered()) {
		capabilities.setSelectionRangeProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isHoverDynamicRegistered()) {
		capabilities.setHoverProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isCompletionDynamicRegistered()) {
		capabilities.setCompletionProvider(CompletionHandler.DEFAULT_COMPLETION_OPTIONS);
	}
	TextDocumentSyncOptions textDocumentSyncOptions = new TextDocumentSyncOptions();
	textDocumentSyncOptions.setOpenClose(Boolean.TRUE);
	textDocumentSyncOptions.setSave(new SaveOptions(Boolean.TRUE));
	textDocumentSyncOptions.setChange(TextDocumentSyncKind.Incremental);
	capabilities.setTextDocumentSync(textDocumentSyncOptions);

	WorkspaceServerCapabilities wsCapabilities = new WorkspaceServerCapabilities();
	WorkspaceFoldersOptions wsFoldersOptions = new WorkspaceFoldersOptions();
	wsFoldersOptions.setSupported(Boolean.TRUE);
	wsFoldersOptions.setChangeNotifications(Boolean.TRUE);
	wsCapabilities.setWorkspaceFolders(wsFoldersOptions);
	capabilities.setWorkspace(wsCapabilities);

	initializeResult.setCapabilities(capabilities);
}
 
Example #4
Source File: InitHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testWillSaveAndWillSaveWaitUntilCapabilities() throws Exception {
	ClientPreferences mockCapabilies = mock(ClientPreferences.class);
	when(mockCapabilies.isExecuteCommandDynamicRegistrationSupported()).thenReturn(Boolean.TRUE);
	when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies);
	when(mockCapabilies.isWillSaveRegistered()).thenReturn(Boolean.TRUE);
	when(mockCapabilies.isWillSaveWaitUntilRegistered()).thenReturn(Boolean.TRUE);
	InitializeResult result = initialize(true);
	Either<TextDocumentSyncKind, TextDocumentSyncOptions> o = result.getCapabilities().getTextDocumentSync();
	assertTrue(o.isRight());
	assertTrue(o.getRight().getWillSave());
	assertTrue(o.getRight().getWillSaveWaitUntil());
}
 
Example #5
Source File: ServerCapabilities.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void setTextDocumentSync(final TextDocumentSyncOptions textDocumentSync) {
  if (textDocumentSync == null) {
    this.textDocumentSync = null;
    return;
  }
  this.textDocumentSync = Either.forRight(textDocumentSync);
}
 
Example #6
Source File: LanguageServerWrapper.java    From lsp4intellij with Apache License 2.0 4 votes vote down vote up
/**
 * Connects an editor to the languageServer
 *
 * @param editor the editor
 */
public void connect(Editor editor) {
    if (editor == null) {
        LOG.warn("editor is null for " + serverDefinition);
        return;
    }
    if (!FileUtils.isEditorSupported(editor)) {
        LOG.debug("Editor hosts a unsupported file type by the LS library.");
        return;
    }

    String uri = editorToURIString(editor);
    uriToLanguageServerWrapper.put(new MutablePair<>(uri, editorToProjectFolderUri(editor)), this);
    if (connectedEditors.containsKey(uri)) {
        return;
    }
    start();
    if (initializeFuture != null) {
        ServerCapabilities capabilities = getServerCapabilities();
        if (capabilities == null) {
            LOG.warn("Capabilities are null for " + serverDefinition);
            return;
        }

        initializeFuture.thenRun(() -> {
            if (connectedEditors.containsKey(uri)) {
                return;
            }
            try {
                Either<TextDocumentSyncKind, TextDocumentSyncOptions> syncOptions = capabilities.getTextDocumentSync();
                if (syncOptions != null) {
                    //Todo - Implement
                    //  SelectionListenerImpl selectionListener = new SelectionListenerImpl();
                    DocumentListenerImpl documentListener = new DocumentListenerImpl();
                    EditorMouseListenerImpl mouseListener = new EditorMouseListenerImpl();
                    EditorMouseMotionListenerImpl mouseMotionListener = new EditorMouseMotionListenerImpl();
                    LSPCaretListenerImpl caretListener = new LSPCaretListenerImpl();

                    ServerOptions serverOptions = new ServerOptions(capabilities);
                    EditorEventManager manager;
                    if (extManager != null) {
                        manager = extManager.getExtendedEditorEventManagerFor(editor, documentListener,
                                mouseListener, mouseMotionListener, caretListener, requestManager, serverOptions,
                                this);
                        if (manager == null) {
                            manager = new EditorEventManager(editor, documentListener, mouseListener,
                                    mouseMotionListener, caretListener,
                                    requestManager, serverOptions, this);
                        }
                    } else {
                        manager = new EditorEventManager(editor, documentListener, mouseListener,
                                mouseMotionListener, caretListener,
                                requestManager, serverOptions, this);
                    }
                    // selectionListener.setManager(manager);
                    documentListener.setManager(manager);
                    mouseListener.setManager(manager);
                    mouseMotionListener.setManager(manager);
                    caretListener.setManager(manager);
                    manager.registerListeners();
                    connectedEditors.put(uri, manager);
                    manager.documentOpened();
                    LOG.info("Created a manager for " + uri);
                    synchronized (toConnect) {
                        toConnect.remove(editor);
                    }
                    for (Editor ed : toConnect) {
                        connect(ed);
                    }
                    // Triggers annotators since this is the first editor which starts the LS
                    // and annotators are executed before LS is bootstrap to provide diagnostics.
                    computableReadAction(() -> {
                        PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
                        if (psiFile != null) {
                            DaemonCodeAnalyzer.getInstance(project).restart(psiFile);
                        }
                        return null;
                    });
                }
            } catch (Exception e) {
                LOG.error(e);
            }
        });

    } else {
        synchronized (toConnect) {
            toConnect.add(editor);
        }
    }
}
 
Example #7
Source File: LanguageServerWrapper.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * To make public when we support non IFiles
 *
 * @return null if not connection has happened, a future that completes when file is initialized otherwise
 * @noreference internal so far
 */
private CompletableFuture<LanguageServer> connect(@Nonnull URI absolutePath, Document document) throws IOException {
    final URI thePath = absolutePath; // should be useless

    VirtualFile file = FileDocumentManager.getInstance().getFile(document);
    if (file != null && file.exists()) {
        watchProject(LSPIJUtils.getProject(file), false);
    }

    if (this.connectedDocuments.containsKey(thePath)) {
        return CompletableFuture.completedFuture(languageServer);
    }
    start();
    if (this.initializeFuture == null) {
        return null;
    }
    if (document == null) {
        VirtualFile docFile = LSPIJUtils.findResourceFor(thePath);
        document = LSPIJUtils.getDocument(docFile);
    }
    if (document == null) {
        return null;
    }
    final Document theDocument = document;
    return initializeFuture.thenComposeAsync(theVoid -> {
        synchronized (connectedDocuments) {
            if (this.connectedDocuments.containsKey(thePath)) {
                return CompletableFuture.completedFuture(null);
            }
            Either<TextDocumentSyncKind, TextDocumentSyncOptions> syncOptions = initializeFuture == null ? null
                    : this.serverCapabilities.getTextDocumentSync();
            TextDocumentSyncKind syncKind = null;
            if (syncOptions != null) {
                if (syncOptions.isRight()) {
                    syncKind = syncOptions.getRight().getChange();
                } else if (syncOptions.isLeft()) {
                    syncKind = syncOptions.getLeft();
                }
            }
            DocumentContentSynchronizer listener = new DocumentContentSynchronizer(this, theDocument, syncKind);
            theDocument.addDocumentListener(listener);
            LanguageServerWrapper.this.connectedDocuments.put(thePath, listener);
            return listener.didOpenFuture;
        }
    }).thenApply(theVoid -> languageServer);
}
 
Example #8
Source File: InitHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void registerCapabilities(InitializeResult initializeResult) {
	ServerCapabilities capabilities = new ServerCapabilities();
	if (!preferenceManager.getClientPreferences().isCompletionDynamicRegistered()) {
		capabilities.setCompletionProvider(CompletionHandler.DEFAULT_COMPLETION_OPTIONS);
	}
	if (!preferenceManager.getClientPreferences().isFormattingDynamicRegistrationSupported()) {
		capabilities.setDocumentFormattingProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isRangeFormattingDynamicRegistrationSupported()) {
		capabilities.setDocumentRangeFormattingProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isOnTypeFormattingDynamicRegistrationSupported()) {
		capabilities.setDocumentOnTypeFormattingProvider(new DocumentOnTypeFormattingOptions(";", Arrays.asList("\n", "}")));
	}
	if (!preferenceManager.getClientPreferences().isCodeLensDynamicRegistrationSupported()) {
		capabilities.setCodeLensProvider(new CodeLensOptions(true));
	}
	if (!preferenceManager.getClientPreferences().isSignatureHelpDynamicRegistrationSupported()) {
		capabilities.setSignatureHelpProvider(SignatureHelpHandler.createOptions());
	}
	if (!preferenceManager.getClientPreferences().isRenameDynamicRegistrationSupported()) {
		capabilities.setRenameProvider(RenameHandler.createOptions());
	}
	if (!preferenceManager.getClientPreferences().isCodeActionDynamicRegistered()) {
		capabilities.setCodeActionProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isExecuteCommandDynamicRegistrationSupported()) {
		Set<String> commands = commandHandler.getAllCommands();
		if (!commands.isEmpty()) {
			capabilities.setExecuteCommandProvider(new ExecuteCommandOptions(new ArrayList<>(commands)));
		}
	} else {
		// Send static command at the startup - they remain registered all the time
		Set<String> staticCommands = commandHandler.getStaticCommands();
		if (!staticCommands.isEmpty()) {
			capabilities.setExecuteCommandProvider(new ExecuteCommandOptions(new ArrayList<>(staticCommands)));
		}
	}
	if (!preferenceManager.getClientPreferences().isWorkspaceSymbolDynamicRegistered()) {
		capabilities.setWorkspaceSymbolProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isClientDocumentSymbolProviderRegistered() && !preferenceManager.getClientPreferences().isDocumentSymbolDynamicRegistered()) {
		capabilities.setDocumentSymbolProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isDefinitionDynamicRegistered()) {
		capabilities.setDefinitionProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isTypeDefinitionDynamicRegistered()) {
		capabilities.setTypeDefinitionProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isClientHoverProviderRegistered() && !preferenceManager.getClientPreferences().isHoverDynamicRegistered()) {
		capabilities.setHoverProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isReferencesDynamicRegistered()) {
		capabilities.setReferencesProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isDocumentHighlightDynamicRegistered()) {
		capabilities.setDocumentHighlightProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isFoldgingRangeDynamicRegistered()) {
		capabilities.setFoldingRangeProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isImplementationDynamicRegistered()) {
		capabilities.setImplementationProvider(Boolean.TRUE);
	}
	if (!preferenceManager.getClientPreferences().isSelectionRangeDynamicRegistered()) {
		capabilities.setSelectionRangeProvider(Boolean.TRUE);
	}
	capabilities.setCallHierarchyProvider(Boolean.TRUE);
	TextDocumentSyncOptions textDocumentSyncOptions = new TextDocumentSyncOptions();
	textDocumentSyncOptions.setOpenClose(Boolean.TRUE);
	textDocumentSyncOptions.setSave(new SaveOptions(Boolean.TRUE));
	textDocumentSyncOptions.setChange(TextDocumentSyncKind.Incremental);
	if (preferenceManager.getClientPreferences().isWillSaveRegistered()) {
		textDocumentSyncOptions.setWillSave(Boolean.TRUE);
	}

	if (preferenceManager.getClientPreferences().isWillSaveWaitUntilRegistered()) {
		textDocumentSyncOptions.setWillSaveWaitUntil(Boolean.TRUE);
	}
	capabilities.setTextDocumentSync(textDocumentSyncOptions);

	if (preferenceManager.getClientPreferences().isSemanticHighlightingSupported()) {
		SemanticHighlightingServerCapabilities semanticHighlightingCapabilities = new SemanticHighlightingServerCapabilities();
		semanticHighlightingCapabilities.setScopes(SemanticHighlightingService.getAllScopes());
		capabilities.setSemanticHighlighting(semanticHighlightingCapabilities);
	}

	WorkspaceServerCapabilities wsCapabilities = new WorkspaceServerCapabilities();
	WorkspaceFoldersOptions wsFoldersOptions = new WorkspaceFoldersOptions();
	wsFoldersOptions.setSupported(Boolean.TRUE);
	wsFoldersOptions.setChangeNotifications(Boolean.TRUE);
	wsCapabilities.setWorkspaceFolders(wsFoldersOptions);
	capabilities.setWorkspace(wsCapabilities);

	initializeResult.setCapabilities(capabilities);
}
 
Example #9
Source File: ServerCapabilities.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Defines how text documents are synced. Is either a detailed structure defining each notification or
 * for backwards compatibility the TextDocumentSyncKind number.
 */
@Pure
public Either<TextDocumentSyncKind, TextDocumentSyncOptions> getTextDocumentSync() {
  return this.textDocumentSync;
}
 
Example #10
Source File: ServerCapabilities.java    From lsp4j with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Defines how text documents are synced. Is either a detailed structure defining each notification or
 * for backwards compatibility the TextDocumentSyncKind number.
 */
public void setTextDocumentSync(final Either<TextDocumentSyncKind, TextDocumentSyncOptions> textDocumentSync) {
  this.textDocumentSync = textDocumentSync;
}