Search in sources :

Example 1 with TextDocumentSyncOptions

use of org.eclipse.lsp4j.TextDocumentSyncOptions in project sonarlint-core by SonarSource.

the class SonarLintLanguageServer method initialize.

@Override
public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
    String rootUri = params.getRootUri();
    // rootURI is null when no folder is open (like opening a single file in VSCode)
    if (rootUri != null) {
        try {
            workspaceRoot = new URI(rootUri);
        } catch (URISyntaxException e) {
            throw new IllegalStateException(e);
        }
    }
    Map<String, Object> options = (Map<String, Object>) params.getInitializationOptions();
    userSettings = new UserSettings(options);
    String productKey = (String) options.get("productKey");
    // deprecated, will be ignored when productKey present
    String telemetryStorage = (String) options.get("telemetryStorage");
    String productName = (String) options.get("productName");
    String productVersion = (String) options.get("productVersion");
    telemetry.init(getStoragePath(productKey, telemetryStorage), productName, productVersion);
    telemetry.optOut(userSettings.disableTelemetry);
    info("Starting SonarLint engine...");
    info("Using " + analyzers.size() + " analyzers");
    try {
        Map<String, String> extraProperties = new HashMap<>();
        extraProperties.put("sonar.typescript.internal.typescriptLocation", (String) options.get(TYPESCRIPT_LOCATION));
        Builder builder = StandaloneGlobalConfiguration.builder().setLogOutput(logOutput).setExtraProperties(extraProperties).addPlugins(analyzers.toArray(new URL[0]));
        this.engine = new StandaloneSonarLintEngineImpl(builder.build());
    } catch (Exception e) {
        error("Error starting SonarLint engine", e);
        throw new IllegalStateException(e);
    }
    info("SonarLint engine started");
    InitializeResult result = new InitializeResult();
    ServerCapabilities c = new ServerCapabilities();
    TextDocumentSyncOptions textDocumentSyncOptions = new TextDocumentSyncOptions();
    textDocumentSyncOptions.setOpenClose(true);
    textDocumentSyncOptions.setChange(TextDocumentSyncKind.Full);
    textDocumentSyncOptions.setSave(new SaveOptions(true));
    c.setTextDocumentSync(textDocumentSyncOptions);
    c.setCodeActionProvider(true);
    c.setExecuteCommandProvider(new ExecuteCommandOptions(asList(SONARLINT_OPEN_RULE_DESCRIPTION_COMMAND)));
    result.setCapabilities(c);
    return CompletableFuture.completedFuture(result);
}
Also used : TextDocumentSyncOptions(org.eclipse.lsp4j.TextDocumentSyncOptions) HashMap(java.util.HashMap) Builder(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration.Builder) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) StandaloneSonarLintEngineImpl(org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl) InitializeResult(org.eclipse.lsp4j.InitializeResult) ServerCapabilities(org.eclipse.lsp4j.ServerCapabilities) SaveOptions(org.eclipse.lsp4j.SaveOptions) ExecuteCommandOptions(org.eclipse.lsp4j.ExecuteCommandOptions) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with TextDocumentSyncOptions

use of org.eclipse.lsp4j.TextDocumentSyncOptions in project eclipse.jdt.ls by eclipse.

the class InitHandlerTest method testWillSaveAndWillSaveWaitUntilCapabilities.

@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());
}
Also used : InitializeResult(org.eclipse.lsp4j.InitializeResult) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) TextDocumentSyncKind(org.eclipse.lsp4j.TextDocumentSyncKind) TextDocumentSyncOptions(org.eclipse.lsp4j.TextDocumentSyncOptions) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 3 with TextDocumentSyncOptions

use of org.eclipse.lsp4j.TextDocumentSyncOptions in project eclipse.jdt.ls by eclipse.

the class InitHandler method initialize.

@SuppressWarnings("unchecked")
InitializeResult initialize(InitializeParams param) {
    logInfo("Initializing Java Language Server " + JavaLanguageServerPlugin.getVersion());
    Map<?, ?> initializationOptions = this.getInitializationOptions(param);
    Map<String, Object> extendedClientCapabilities = getInitializationOption(initializationOptions, "extendedClientCapabilities", Map.class);
    if (param.getCapabilities() == null) {
        preferenceManager.updateClientPrefences(new ClientCapabilities(), extendedClientCapabilities);
    } else {
        preferenceManager.updateClientPrefences(param.getCapabilities(), extendedClientCapabilities);
    }
    Collection<IPath> rootPaths = new ArrayList<>();
    Collection<String> workspaceFolders = getInitializationOption(initializationOptions, "workspaceFolders", Collection.class);
    if (workspaceFolders != null && !workspaceFolders.isEmpty()) {
        for (String uri : workspaceFolders) {
            IPath filePath = ResourceUtils.filePathFromURI(uri);
            if (filePath != null) {
                rootPaths.add(filePath);
            }
        }
    } else {
        String rootPath = param.getRootUri();
        if (rootPath == null) {
            rootPath = param.getRootPath();
            if (rootPath != null) {
                logInfo("In LSP 3.0, InitializeParams.rootPath is deprecated in favour of InitializeParams.rootUri!");
            }
        }
        if (rootPath != null) {
            IPath filePath = ResourceUtils.filePathFromURI(rootPath);
            if (filePath != null) {
                rootPaths.add(filePath);
            }
        }
    }
    if (rootPaths.isEmpty()) {
        IPath workspaceLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation();
        logInfo("No workspace folders or root uri was defined. Falling back on " + workspaceLocation);
        rootPaths.add(workspaceLocation);
    }
    if (initializationOptions.get(SETTINGS_KEY) instanceof Map) {
        Object settings = initializationOptions.get(SETTINGS_KEY);
        @SuppressWarnings("unchecked") Preferences prefs = Preferences.createFrom((Map<String, Object>) settings);
        preferenceManager.update(prefs);
    }
    triggerInitialization(rootPaths);
    addWorkspaceDiagnosticsHandler();
    Integer processId = param.getProcessId();
    if (processId != null) {
        JavaLanguageServerPlugin.getLanguageServer().setParentProcessId(processId.longValue());
    }
    try {
        Collection<String> bundleList = getInitializationOption(initializationOptions, BUNDLES_KEY, Collection.class);
        BundleUtils.loadBundles(bundleList);
    } catch (CoreException e) {
        // The additional plug-ins should not affect the main language server loading.
        JavaLanguageServerPlugin.logException("Failed to load extension bundles ", e);
    }
    InitializeResult result = new InitializeResult();
    ServerCapabilities capabilities = new ServerCapabilities();
    capabilities.setCompletionProvider(new CompletionOptions(Boolean.TRUE, Arrays.asList(".", "@", "#")));
    if (!preferenceManager.getClientPreferences().isFormattingDynamicRegistrationSupported()) {
        capabilities.setDocumentFormattingProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isRangeFormattingDynamicRegistrationSupported()) {
        capabilities.setDocumentRangeFormattingProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isCodeLensDynamicRegistrationSupported()) {
        capabilities.setCodeLensProvider(new CodeLensOptions(true));
    }
    if (!preferenceManager.getClientPreferences().isSignatureHelpDynamicRegistrationSupported()) {
        capabilities.setSignatureHelpProvider(SignatureHelpHandler.createOptions());
    }
    if (!preferenceManager.getClientPreferences().isRenameDynamicRegistrationSupported()) {
        capabilities.setRenameProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isCodeActionDynamicRegistered()) {
        capabilities.setCodeActionProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isExecuteCommandDynamicRegistrationSupported()) {
        Set<String> commands = WorkspaceExecuteCommandHandler.getCommands();
        capabilities.setExecuteCommandProvider(new ExecuteCommandOptions(new ArrayList<>(commands)));
    }
    if (!preferenceManager.getClientPreferences().isWorkspaceSymbolDynamicRegistered()) {
        capabilities.setWorkspaceSymbolProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isDocumentSymbolDynamicRegistered()) {
        capabilities.setDocumentSymbolProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isDefinitionDynamicRegistered()) {
        capabilities.setDefinitionProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isHoverDynamicRegistered()) {
        capabilities.setHoverProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isReferencesDynamicRegistered()) {
        capabilities.setReferencesProvider(Boolean.TRUE);
    }
    if (!preferenceManager.getClientPreferences().isDocumentHighlightDynamicRegistered()) {
        capabilities.setDocumentHighlightProvider(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);
    WorkspaceServerCapabilities wsCapabilities = new WorkspaceServerCapabilities();
    WorkspaceFoldersOptions wsFoldersOptions = new WorkspaceFoldersOptions();
    wsFoldersOptions.setSupported(Boolean.TRUE);
    wsFoldersOptions.setChangeNotifications(Boolean.TRUE);
    wsCapabilities.setWorkspaceFolders(wsFoldersOptions);
    capabilities.setWorkspace(wsCapabilities);
    result.setCapabilities(capabilities);
    return result;
}
Also used : IPath(org.eclipse.core.runtime.IPath) TextDocumentSyncOptions(org.eclipse.lsp4j.TextDocumentSyncOptions) ArrayList(java.util.ArrayList) ClientCapabilities(org.eclipse.lsp4j.ClientCapabilities) WorkspaceFoldersOptions(org.eclipse.lsp4j.WorkspaceFoldersOptions) WorkspaceServerCapabilities(org.eclipse.lsp4j.WorkspaceServerCapabilities) CompletionOptions(org.eclipse.lsp4j.CompletionOptions) InitializeResult(org.eclipse.lsp4j.InitializeResult) CoreException(org.eclipse.core.runtime.CoreException) ServerCapabilities(org.eclipse.lsp4j.ServerCapabilities) WorkspaceServerCapabilities(org.eclipse.lsp4j.WorkspaceServerCapabilities) CodeLensOptions(org.eclipse.lsp4j.CodeLensOptions) SaveOptions(org.eclipse.lsp4j.SaveOptions) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) ExecuteCommandOptions(org.eclipse.lsp4j.ExecuteCommandOptions) Map(java.util.Map)

Aggregations

InitializeResult (org.eclipse.lsp4j.InitializeResult)3 TextDocumentSyncOptions (org.eclipse.lsp4j.TextDocumentSyncOptions)3 Map (java.util.Map)2 ExecuteCommandOptions (org.eclipse.lsp4j.ExecuteCommandOptions)2 SaveOptions (org.eclipse.lsp4j.SaveOptions)2 ServerCapabilities (org.eclipse.lsp4j.ServerCapabilities)2 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)1 ClientPreferences (org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences)1 Preferences (org.eclipse.jdt.ls.core.internal.preferences.Preferences)1 ClientCapabilities (org.eclipse.lsp4j.ClientCapabilities)1 CodeLensOptions (org.eclipse.lsp4j.CodeLensOptions)1 CompletionOptions (org.eclipse.lsp4j.CompletionOptions)1