Skip to content

Commit

Permalink
Bug 536320 - Do not show LSP4E diagnostics in the C++ editor
Browse files Browse the repository at this point in the history
Requires https://git.eclipse.org/r/c/lsp4e/lsp4e/+/168414
Allow LS to be started
	from "Generic Editor" only
		and
	if ".settings/org.eclipse.cdt.lsp.core.prefs" has "prefer=false"
actually this blocks LSP from launching until user will configure it
explicitly.
UI to configure for workspace and project.

Change-Id: I7104d21f90380a5de7ccae157ea4299e0558a27c
Signed-off-by: Alexander Fedorov <alexander.fedorov@arsysop.ru>
  • Loading branch information
ruspl-afed committed Sep 2, 2020
1 parent 718088b commit 4a3e046
Show file tree
Hide file tree
Showing 23 changed files with 501 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp.text.tests;
package org.eclipse.cdt.lsp.internal.core.workspace.tests;

import static org.junit.Assert.assertFalse;

import org.eclipse.cdt.lsp.internal.text.ResolveDocumentUri;
import org.eclipse.cdt.lsp.internal.core.workspace.ResolveDocumentUri;
import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.core.runtime.Path;
Expand Down
5 changes: 4 additions & 1 deletion lsp/org.eclipse.cdt.lsp.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Bundle-Vendor: %Bundle-Vendor
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: com.google.gson;bundle-version="2.8.2",
org.eclipse.cdt.core,
org.eclipse.core.filebuffers;bundle-version="3.6.1000",
org.eclipse.core.resources,
org.eclipse.core.runtime,
org.eclipse.lsp4e,
Expand All @@ -18,8 +19,10 @@ Require-Bundle: com.google.gson;bundle-version="2.8.2",
org.eclipse.ui
Export-Package: org.eclipse.cdt.lsp;x-friends:="org.eclipse.cdt.lsp.ui,org.eclipse.cdt.lsp.clangd",
org.eclipse.cdt.lsp.core;x-friends:="org.eclipse.cdt.lsp.ui",
org.eclipse.cdt.lsp.core.preferences;x-friends:="org.eclipse.cdt.lsp.ui",
org.eclipse.cdt.lsp.internal.core;x-internal:=true,
org.eclipse.cdt.lsp.internal.text;x-friends:="org.eclipse.cdt.lsp.ui"
org.eclipse.cdt.lsp.internal.core.preferences;x-friends:="org.eclipse.cdt.lsp.ui",
org.eclipse.cdt.lsp.internal.core.workspace;x-friends:="org.eclipse.cdt.lsp.ui"
Bundle-Activator: org.eclipse.cdt.lsp.core.Activator
Bundle-ActivationPolicy: lazy
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.internal.core.ContributedLanguageServers.xml,
Expand Down
24 changes: 24 additions & 0 deletions lsp/org.eclipse.cdt.lsp.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<contentTypeMapping
contentType="org.eclipse.cdt.lsp.core"
id="org.eclipse.cdt.lsp.core">
<enabledWhen
description="Project-level settings">
<reference
definitionId="org.eclipse.cdt.lsp.core.projectAllows">
</reference>
</enabledWhen>
</contentTypeMapping>
</extension>
<extension
Expand All @@ -44,4 +50,22 @@
class="org.eclipse.cdt.lsp.core.PreferenceInitializer">
</initializer>
</extension>
<extension
point="org.eclipse.core.expressions.definitions">
<definition
id="org.eclipse.cdt.lsp.core.projectAllows">
<and>
<reference
definitionId="org.eclipse.ui.genericeditor.GenericEditor.active">
</reference>
<with
variable="activeEditor">
<test
property="org.eclipse.cdt.lsp.documents.prefer"
value="true">
</test>
</with>
</and>
</definition>
</extension>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp.core.preferences;

import org.eclipse.core.runtime.preferences.PreferenceMetadata;

/**
* The metadata for preferences to configure language server
*
*/
public interface LanguageServerPreferenceMetadata {

/**
* Returns the metadata for the "Prefer Language Server" preference, must not return <code>null</code>.
*
* @return the metadata for the "Prefer Language Server" preference
*
* @see LanguageServerPreferences#preferLanguageServer()
*/
PreferenceMetadata<Boolean> preferLanguageServer();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Eclipse Foundation and others.
* Copyright (c) 2019, 2020 Eclipse Contributors and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -8,7 +8,7 @@
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov <alexander.fedorov@arsysop.ru> - initial API and implementatin
* Alexander Fedorov <alexander.fedorov@arsysop.ru> - ongoing support
*******************************************************************************/
package org.eclipse.cdt.lsp.internal.core;

Expand All @@ -23,6 +23,8 @@ public class LspCoreMessages extends NLS {
NLS.initializeMessages(BUNDLE_NAME, LspCoreMessages.class);
}

public static String LanguageServerDefaults_prefer_description;
public static String LanguageServerDefaults_prefer_name;
public static String ShowStatus_busy;
public static String ShowStatus_idle;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###############################################################################
# Copyright (c) 2019 Eclipse Foundation and others
# Copyright (c) 2019, 2020 Eclipse Contributors and others
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -8,9 +8,10 @@
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Eclipse Foundation - initial API and implementation
# Alexander Fedorov <alexander.fedorov@arsysop.ru> - Bug 558484
# Alexander Fedorov <alexander.fedorov@arsysop.ru> - ongoing support
###############################################################################

LanguageServerDefaults_prefer_description=Prefer to use language server instead of "classic" facilities
LanguageServerDefaults_prefer_name=Prefer Language Server
ShowStatus_busy={0} : Busy | {1} Jobs
ShowStatus_idle={0} : Idle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp.internal.core.preferences;

import org.eclipse.cdt.lsp.core.preferences.LanguageServerPreferenceMetadata;
import org.eclipse.cdt.lsp.internal.core.LspCoreMessages;
import org.eclipse.core.runtime.preferences.PreferenceMetadata;

public class LanguageServerDefaults implements LanguageServerPreferenceMetadata {

@Override
public PreferenceMetadata<Boolean> preferLanguageServer() {
return new PreferenceMetadata<>(Boolean.class, "prefer_language_server", false, //$NON-NLS-1$
LspCoreMessages.LanguageServerDefaults_prefer_name,
LspCoreMessages.LanguageServerDefaults_prefer_description);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp.internal.core.workspace;

import java.util.Optional;
import java.util.function.Predicate;

import org.eclipse.cdt.lsp.core.Activator;
import org.eclipse.cdt.lsp.core.preferences.LanguageServerPreferenceMetadata;
import org.eclipse.cdt.lsp.internal.core.preferences.LanguageServerDefaults;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.PreferenceMetadata;
import org.eclipse.jface.text.IDocument;

public final class PreferLanguageServer implements Predicate<IDocument> {

private final ResolveDocumentFile file;
private final LanguageServerPreferenceMetadata metadata;

public PreferLanguageServer() {
file = new ResolveDocumentFile();
metadata = new LanguageServerDefaults();
}

@Override
public boolean test(IDocument document) {
Optional<IProject> project = file.apply(document).map(IFile::getProject);
if (project.isPresent()) {
return forProject(project.get());
}
return forExternal(document);
}

private boolean forExternal(IDocument document) {
//let's use workspace-level setting
PreferenceMetadata<Boolean> option = metadata.preferLanguageServer();
return Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, option.identifer(),
option.defaultValue(), null);
}

private boolean forProject(IProject project) {
PreferenceMetadata<Boolean> option = metadata.preferLanguageServer();
return Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, option.identifer(),
option.defaultValue(), new IScopeContext[] { new ProjectScope(project) });
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp.internal.core.workspace;

import java.util.Optional;
import java.util.function.Function;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.jface.text.IDocument;

public final class ResolveDocumentBuffer implements Function<IDocument, Optional<ITextFileBuffer>> {

@Override
public Optional<ITextFileBuffer> apply(IDocument document) {
return Optional.ofNullable(document).map(FileBuffers.getTextFileBufferManager()::getTextFileBuffer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp.internal.core.workspace;

import java.util.Optional;
import java.util.function.Function;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.text.IDocument;

public final class ResolveDocumentFile implements Function<IDocument, Optional<IFile>> {

private final ResolveDocumentPath path;

public ResolveDocumentFile() {
path = new ResolveDocumentPath();
}

@Override
public Optional<IFile> apply(IDocument document) {
return Optional.ofNullable(document)//
.flatMap(path)//
.map(ResourcesPlugin.getWorkspace().getRoot()::findMember)//
.filter(IFile.class::isInstance)//
.map(IFile.class::cast);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp.internal.core.workspace;

import java.util.Optional;
import java.util.function.Function;

import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.text.IDocument;

public final class ResolveDocumentPath implements Function<IDocument, Optional<IPath>> {

private final ResolveDocumentBuffer buffer;

public ResolveDocumentPath() {
buffer = new ResolveDocumentBuffer();
}

@Override
public Optional<IPath> apply(IDocument document) {
return Optional.ofNullable(document).flatMap(buffer).map(ITextFileBuffer::getLocation);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp.internal.text;
package org.eclipse.cdt.lsp.internal.core.workspace;

import java.net.URI;
import java.util.Optional;
Expand All @@ -23,11 +23,17 @@
@SuppressWarnings("restriction")
public final class ResolveDocumentUri implements Function<IDocument, Optional<URI>> {

private final ResolveDocumentFile file;

public ResolveDocumentUri() {
file = new ResolveDocumentFile();
}

@Override
public Optional<URI> apply(IDocument document) {
return Optional.ofNullable(document)//
//FIXME rewrite involved static utilities and contribute the result back to LSP4E
.flatMap(d -> Optional.ofNullable(LSPEclipseUtils.getFile(d)))
.flatMap(file)//
//FIXME rewrite involved static utilities
.flatMap(f -> Optional.ofNullable(LSPEclipseUtils.toUri(f)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager.HighlightedPosition;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager.HighlightingStyle;
import org.eclipse.cdt.lsp.internal.core.workspace.ResolveDocumentUri;
import org.eclipse.cdt.lsp.internal.cquery.CquerySemanticHighlights;
import org.eclipse.cdt.lsp.internal.cquery.HighlightSymbol;
import org.eclipse.cdt.lsp.internal.text.ResolveDocumentUri;
import org.eclipse.cdt.lsp.internal.ui.text.PresentationReconcilerCPP;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
Expand Down
1 change: 1 addition & 0 deletions lsp/org.eclipse.cdt.lsp.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: org.eclipse.osgi;bundle-version="[3.15.0,4.0.0)",
org.eclipse.core.expressions;bundle-version="3.7.0",
org.eclipse.core.resources;bundle-version="3.13.800",
org.eclipse.core.runtime;bundle-version="3.19.0",
org.eclipse.equinox.preferences;bundle-version="[3.7.0,4.0.0)",
Expand Down
Loading

0 comments on commit 4a3e046

Please sign in to comment.