Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check if WorkspaceFolders are supported or not #2801

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void testExecute() throws Exception {
initialize((InitializeParams it) -> {
ClientCapabilities clientCapabilities = new ClientCapabilities();
WorkspaceClientCapabilities workspaceClientCapabilities = new WorkspaceClientCapabilities();
workspaceClientCapabilities.setWorkspaceFolders(true);
workspaceClientCapabilities.setExecuteCommand(new ExecuteCommandCapabilities(true));
clientCapabilities.setWorkspace(workspaceClientCapabilities);
it.setCapabilities(clientCapabilities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
import org.eclipse.lsp4j.DocumentSymbolCapabilities;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.TextDocumentClientCapabilities;
import org.eclipse.lsp4j.WorkspaceClientCapabilities;
import org.eclipse.xtext.testing.DocumentSymbolConfiguration;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.junit.Test;

public class HierarchicalDocumentSymbolTest extends AbstractTestLangLanguageServerTest {
private static final Procedure1<? super InitializeParams> INITIALIZER = (InitializeParams it) -> {
ClientCapabilities clientCapabilities = new ClientCapabilities();
WorkspaceClientCapabilities workspaceClientCapabilities = new WorkspaceClientCapabilities();
workspaceClientCapabilities.setWorkspaceFolders(true);
clientCapabilities.setWorkspace(workspaceClientCapabilities);
DocumentSymbolCapabilities documentSymbolCapabilities = new DocumentSymbolCapabilities();
documentSymbolCapabilities.setHierarchicalDocumentSymbolSupport(true);
TextDocumentClientCapabilities textDocumentClientCapabilities = new TextDocumentClientCapabilities();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ private InitializeResult initializeWithPrepareSupport() {
return super.initialize((InitializeParams params) -> {
ClientCapabilities clientCapabilities = new ClientCapabilities();
WorkspaceClientCapabilities workspaceClientCapabilities = new WorkspaceClientCapabilities();
workspaceClientCapabilities.setWorkspaceFolders(true);
clientCapabilities.setWorkspace(workspaceClientCapabilities);
TextDocumentClientCapabilities textDocumentClientCapabilities = new TextDocumentClientCapabilities();
textDocumentClientCapabilities.setRename(new RenameCapabilities(true, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ protected InitializeResult initialize() {
return super.initialize((InitializeParams params) -> {
ClientCapabilities clientCapabilities = new ClientCapabilities();
WorkspaceClientCapabilities workspaceClientCapabilities = new WorkspaceClientCapabilities();
workspaceClientCapabilities.setWorkspaceFolders(true);
WorkspaceEditCapabilities workspaceEditCapabilities = new WorkspaceEditCapabilities();
workspaceEditCapabilities.setDocumentChanges(true);
workspaceClientCapabilities.setWorkspaceEdit(workspaceEditCapabilities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ protected InitializeResult initialize() {
return super.initialize((InitializeParams params) -> {
ClientCapabilities clientCapabilities = new ClientCapabilities();
WorkspaceClientCapabilities workspaceClientCapabilities = new WorkspaceClientCapabilities();
workspaceClientCapabilities.setWorkspaceFolders(true);
WorkspaceEditCapabilities workspaceEditCapabilities = new WorkspaceEditCapabilities();
workspaceEditCapabilities.setDocumentChanges(true);
workspaceClientCapabilities.setWorkspaceEdit(workspaceEditCapabilities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.TextDocumentClientCapabilities;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.WorkspaceClientCapabilities;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
import org.eclipse.lsp4j.jsonrpc.messages.Either3;
Expand Down Expand Up @@ -103,6 +104,9 @@ protected void renameWithSuccess(String model, Position position) throws Excepti
String modelFile = writeFile("MyType.testlang", model);
initialize((InitializeParams params) -> {
ClientCapabilities clientCapabilities = new ClientCapabilities();
WorkspaceClientCapabilities workspaceCapabilities = new WorkspaceClientCapabilities();
workspaceCapabilities.setWorkspaceFolders(true);
clientCapabilities.setWorkspace(workspaceCapabilities);
TextDocumentClientCapabilities textDocumentClientCapabilities = new TextDocumentClientCapabilities();
textDocumentClientCapabilities.setRename(new RenameCapabilities(true, false));
clientCapabilities.setTextDocument(textDocumentClientCapabilities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.eclipse.lsp4j.ClientCapabilities;
import org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.NotebookDocumentChangeEvent;
import org.eclipse.lsp4j.WorkspaceClientCapabilities;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.eclipse.lsp4j.WorkspaceFoldersChangeEvent;
import org.eclipse.xtext.ide.server.ILanguageServerAccess;
Expand Down Expand Up @@ -47,16 +51,34 @@ public class WorkspaceFoldersTest extends AbstractTestLangLanguageServerTest {
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void testInitialize() throws Exception {
public void testInitializeWithoutWorkspaceFolders() throws Exception {
File rootFolder1 = temporaryFolder.newFolder("root1");
String oneUri = writeFile(rootFolder1, "one.testlang", "type Foo { Bar bar }");
InitializeResult capabilities = initialize((InitializeParams it) -> {
it.setRootPath(rootFolder1.getPath());
}, true, true);
Assert.assertNull(capabilities.getCapabilities().getWorkspace());
Assert.assertEquals(1, getDiagnostics().size());
Assert.assertEquals(1, getDiagnostics().get(oneUri).size());
}

@Test
public void testInitializeWithWorkspaceFolders() throws Exception {
File rootFolder1 = temporaryFolder.newFolder("root1");
File rootFolder2 = temporaryFolder.newFolder("root2");
writeFile(rootFolder1, "one.testlang", "type Foo { Bar bar }");
String twoUri = writeFile(rootFolder2, "two.testlang", "type Bar { Foo foo }");
initialize((InitializeParams it) -> {
InitializeResult capabilities = initialize((InitializeParams it) -> {
ClientCapabilities clientCapabilities = new ClientCapabilities();
WorkspaceClientCapabilities workspaceClientCapabilities = new WorkspaceClientCapabilities();
workspaceClientCapabilities.setWorkspaceFolders(true);
clientCapabilities.setWorkspace(workspaceClientCapabilities);
it.setCapabilities(clientCapabilities);
it.setWorkspaceFolders(
Lists.newArrayList(new WorkspaceFolder(uriExtensions.toUriString(rootFolder1.toURI()), "root1"),
new WorkspaceFolder(uriExtensions.toUriString(rootFolder2.toURI()), "root2")));
});
Assert.assertEquals(true, capabilities.getCapabilities().getWorkspace().getWorkspaceFolders().getSupported());
Assert.assertEquals(2, getDiagnostics().size());
Assert.assertEquals(1, getDiagnostics().get(twoUri).size());
withBuild(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
if (initializeParams != null) {
throw new IllegalStateException("This language server has already been initialized.");
}
URI baseDir = getBaseDir(params);
if (languagesRegistry.getExtensionToFactoryMap().isEmpty()) {
throw new IllegalStateException(
"No Xtext languages have been registered. Please make sure you have added the languages\'s setup class in \'/META-INF/services/org.eclipse.xtext.ISetup\'");
Expand All @@ -233,18 +232,25 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
result.setCapabilities(createServerCapabilities(params));
access.addBuildListener(this);
return requestManager.runWrite(() -> {
if (workspaceManager.isSupportsWorkspaceFolders()) {
if (clientSupportsWorkspaceFolders() && workspaceManager.isSupportsWorkspaceFolders()) {
List<WorkspaceFolder> workspaceFolders = params.getWorkspaceFolders();
if (workspaceFolders == null)
workspaceFolders = Collections.emptyList();
workspaceManager.initialize(workspaceFolders, this::publishDiagnostics, CancelIndicator.NullImpl);
} else {
URI baseDir = getBaseDir(params);
workspaceManager.initialize(baseDir, this::publishDiagnostics, CancelIndicator.NullImpl);
}
return result;
}, (cancelIndicator, it) -> it).thenApply(it -> initializeResult = it);
}

protected boolean clientSupportsWorkspaceFolders() {
return this.initializeParams.getCapabilities() != null
&& this.initializeParams.getCapabilities().getWorkspace() != null
&& Objects.equal(this.initializeParams.getCapabilities().getWorkspace().getWorkspaceFolders(), Boolean.TRUE);
}

/**
* Configure the server capabilities for this instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ import static extension org.eclipse.xtext.util.Strings.*
import org.eclipse.lsp4j.WorkspaceSymbol
import org.eclipse.lsp4j.SemanticTokensParams
import com.google.common.annotations.Beta
import org.eclipse.lsp4j.ClientCapabilities
import org.eclipse.lsp4j.WorkspaceClientCapabilities

/**
* @author Sven Efftinge - Initial contribution and API
Expand Down Expand Up @@ -217,17 +219,30 @@ abstract class AbstractLanguageServerTest implements Endpoint {
}

protected def InitializeResult initialize((InitializeParams)=>void initializer, boolean callInitialized) {
initialize(initializer, callInitialized, false)
}

protected def InitializeResult initialize((InitializeParams)=>void initializer, boolean callInitialized, boolean useRootPath) {
val params = new InitializeParams => [
processId = 1
workspaceFolders = #[
new WorkspaceFolder(root.toURI.normalize.toUriString, '')
]
]
if (!useRootPath) {
if (params.capabilities === null) {
params.capabilities = new ClientCapabilities
}
if (params.capabilities.workspace === null) {
params.capabilities.workspace = new WorkspaceClientCapabilities
}
params.capabilities.workspace.workspaceFolders = true
}
initializer?.apply(params)
hierarchicalDocumentSymbolSupport = params.capabilities?.textDocument?.documentSymbol?.
hierarchicalDocumentSymbolSupport ?: false;
val result = languageServer.initialize(params).get
if(callInitialized)
if (callInitialized)
languageServer.initialized(null)
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.eclipse.lsp4j.TextDocumentItem;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.VersionedTextDocumentIdentifier;
import org.eclipse.lsp4j.WorkspaceClientCapabilities;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.eclipse.lsp4j.WorkspaceSymbol;
Expand Down Expand Up @@ -333,6 +334,10 @@ protected InitializeResult initialize(final Procedure1<? super InitializeParams>
}

protected InitializeResult initialize(final Procedure1<? super InitializeParams> initializer, final boolean callInitialized) {
return this.initialize(initializer, callInitialized, false);
}

protected InitializeResult initialize(final Procedure1<? super InitializeParams> initializer, final boolean callInitialized, final boolean useRootPath) {
try {
InitializeParams _initializeParams = new InitializeParams();
final Procedure1<InitializeParams> _function = (InitializeParams it) -> {
Expand All @@ -342,14 +347,31 @@ protected InitializeResult initialize(final Procedure1<? super InitializeParams>
it.setWorkspaceFolders(Collections.<WorkspaceFolder>unmodifiableList(CollectionLiterals.<WorkspaceFolder>newArrayList(_workspaceFolder)));
};
final InitializeParams params = ObjectExtensions.<InitializeParams>operator_doubleArrow(_initializeParams, _function);
if ((!useRootPath)) {
ClientCapabilities _capabilities = params.getCapabilities();
boolean _tripleEquals = (_capabilities == null);
if (_tripleEquals) {
ClientCapabilities _clientCapabilities = new ClientCapabilities();
params.setCapabilities(_clientCapabilities);
}
WorkspaceClientCapabilities _workspace = params.getCapabilities().getWorkspace();
boolean _tripleEquals_1 = (_workspace == null);
if (_tripleEquals_1) {
ClientCapabilities _capabilities_1 = params.getCapabilities();
WorkspaceClientCapabilities _workspaceClientCapabilities = new WorkspaceClientCapabilities();
_capabilities_1.setWorkspace(_workspaceClientCapabilities);
}
WorkspaceClientCapabilities _workspace_1 = params.getCapabilities().getWorkspace();
_workspace_1.setWorkspaceFolders(Boolean.valueOf(true));
}
if (initializer!=null) {
initializer.apply(params);
}
Boolean _elvis = null;
ClientCapabilities _capabilities = params.getCapabilities();
ClientCapabilities _capabilities_2 = params.getCapabilities();
TextDocumentClientCapabilities _textDocument = null;
if (_capabilities!=null) {
_textDocument=_capabilities.getTextDocument();
if (_capabilities_2!=null) {
_textDocument=_capabilities_2.getTextDocument();
}
DocumentSymbolCapabilities _documentSymbol = null;
if (_textDocument!=null) {
Expand Down