diff --git a/org.eclipse.xtext.ide.tests/src/org/eclipse/xtext/ide/tests/server/ServerTest.xtend b/org.eclipse.xtext.ide.tests/src/org/eclipse/xtext/ide/tests/server/ServerTest.xtend index c9dec2490..648644a9f 100644 --- a/org.eclipse.xtext.ide.tests/src/org/eclipse/xtext/ide/tests/server/ServerTest.xtend +++ b/org.eclipse.xtext.ide.tests/src/org/eclipse/xtext/ide/tests/server/ServerTest.xtend @@ -8,8 +8,13 @@ package org.eclipse.xtext.ide.tests.server import org.eclipse.lsp4j.DidChangeWatchedFilesParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams import org.eclipse.lsp4j.FileChangeType import org.eclipse.lsp4j.FileEvent +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.junit.Assert import org.junit.Test import static org.junit.Assert.* @@ -67,6 +72,19 @@ class ServerTest extends AbstractTestLangLanguageServerTest { assertTrue(diagnostics.values.join(','), diagnostics.values.forall[empty]) } - + @Test + def void testMissingInitialize() { + try { + languageServer.didOpen(new DidOpenTextDocumentParams => [ + textDocument = new TextDocumentItem => [ + uri = 'file:/home/test/workspace/mydoc.testlang' + text = 'type Foo {}' + ] + ]) + Assert.fail("Expected a ResponseErrorException") + } catch (ResponseErrorException exception) { + Assert.assertEquals(ResponseErrorCode.serverNotInitialized.value, exception.responseError.code) + } + } } \ No newline at end of file diff --git a/org.eclipse.xtext.ide.tests/xtend-gen/org/eclipse/xtext/ide/tests/server/ServerTest.java b/org.eclipse.xtext.ide.tests/xtend-gen/org/eclipse/xtext/ide/tests/server/ServerTest.java index 8021a304b..3e9549259 100644 --- a/org.eclipse.xtext.ide.tests/xtend-gen/org/eclipse/xtext/ide/tests/server/ServerTest.java +++ b/org.eclipse.xtext.ide.tests/xtend-gen/org/eclipse/xtext/ide/tests/server/ServerTest.java @@ -11,14 +11,21 @@ import java.util.Collections; import java.util.List; import org.eclipse.lsp4j.Diagnostic; import org.eclipse.lsp4j.DidChangeWatchedFilesParams; +import org.eclipse.lsp4j.DidOpenTextDocumentParams; import org.eclipse.lsp4j.FileChangeType; import org.eclipse.lsp4j.FileEvent; +import org.eclipse.lsp4j.TextDocumentItem; +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException; +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode; import org.eclipse.lsp4j.services.WorkspaceService; import org.eclipse.xtend2.lib.StringConcatenation; import org.eclipse.xtext.ide.tests.server.AbstractTestLangLanguageServerTest; import org.eclipse.xtext.xbase.lib.CollectionLiterals; +import org.eclipse.xtext.xbase.lib.Exceptions; import org.eclipse.xtext.xbase.lib.Functions.Function1; import org.eclipse.xtext.xbase.lib.IterableExtensions; +import org.eclipse.xtext.xbase.lib.ObjectExtensions; +import org.eclipse.xtext.xbase.lib.Procedures.Procedure1; import org.junit.Assert; import org.junit.Test; @@ -95,4 +102,30 @@ public class ServerTest extends AbstractTestLangLanguageServerTest { }; Assert.assertTrue(IterableExtensions.join(this.getDiagnostics().values(), ","), IterableExtensions.>forall(this.getDiagnostics().values(), _function)); } + + @Test + public void testMissingInitialize() { + try { + DidOpenTextDocumentParams _didOpenTextDocumentParams = new DidOpenTextDocumentParams(); + final Procedure1 _function = (DidOpenTextDocumentParams it) -> { + TextDocumentItem _textDocumentItem = new TextDocumentItem(); + final Procedure1 _function_1 = (TextDocumentItem it_1) -> { + it_1.setUri("file:/home/test/workspace/mydoc.testlang"); + it_1.setText("type Foo {}"); + }; + TextDocumentItem _doubleArrow = ObjectExtensions.operator_doubleArrow(_textDocumentItem, _function_1); + it.setTextDocument(_doubleArrow); + }; + DidOpenTextDocumentParams _doubleArrow = ObjectExtensions.operator_doubleArrow(_didOpenTextDocumentParams, _function); + this.languageServer.didOpen(_doubleArrow); + Assert.fail("Expected a ResponseErrorException"); + } catch (final Throwable _t) { + if (_t instanceof ResponseErrorException) { + final ResponseErrorException exception = (ResponseErrorException)_t; + Assert.assertEquals(ResponseErrorCode.serverNotInitialized.getValue(), exception.getResponseError().getCode()); + } else { + throw Exceptions.sneakyThrow(_t); + } + } + } } diff --git a/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/server/WorkspaceManager.xtend b/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/server/WorkspaceManager.xtend index 823541c63..353b34a5e 100644 --- a/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/server/WorkspaceManager.xtend +++ b/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/server/WorkspaceManager.xtend @@ -16,6 +16,9 @@ import java.util.Map import java.util.Set import org.eclipse.emf.common.util.URI import org.eclipse.lsp4j.TextEdit +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.jsonrpc.messages.ResponseError +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode import org.eclipse.xtext.ide.server.BuildManager.Buildable import org.eclipse.xtext.ide.server.ILanguageServerAccess.IBuildListener import org.eclipse.xtext.resource.IExternalContentSupport.IExternalContentProvider @@ -45,7 +48,7 @@ import org.eclipse.xtext.workspace.IWorkspaceConfig URI baseDir (URI, Iterable)=>void issueAcceptor - IWorkspaceConfig workspaceConfig + IWorkspaceConfig _workspaceConfig List buildListeners = newArrayList @@ -84,7 +87,7 @@ import org.eclipse.xtext.workspace.IWorkspaceConfig refreshWorkspaceConfig(cancelIndicator) } - protected def refreshWorkspaceConfig(CancelIndicator cancelIndicator) { + protected def void refreshWorkspaceConfig(CancelIndicator cancelIndicator) { workspaceConfig = workspaceConfigFactory.getWorkspaceConfig(baseDir) val newProjects = newArrayList val Set remainingProjectNames = new HashSet(projectName2ProjectManager.keySet) @@ -106,8 +109,20 @@ import org.eclipse.xtext.workspace.IWorkspaceConfig val result = buildManager.doInitialBuild(newProjects, cancelIndicator) afterBuild(result) } + + protected def IWorkspaceConfig getWorkspaceConfig() { + if (_workspaceConfig === null) { + val error = new ResponseError(ResponseErrorCode.serverNotInitialized, "Workspace has not been initialized yet.", null) + throw new ResponseErrorException(error) + } + return _workspaceConfig + } + + protected def void setWorkspaceConfig(IWorkspaceConfig workspaceConfig) { + this._workspaceConfig = workspaceConfig + } - protected def afterBuild(List deltas) { + protected def void afterBuild(List deltas) { for (listener : buildListeners) { listener.afterBuild(deltas) } @@ -130,9 +145,9 @@ import org.eclipse.xtext.workspace.IWorkspaceConfig return new ChunkedResourceDescriptions(fullIndex) } - def URI getProjectBaseDir(URI candidate) { - val projectConfig = workspaceConfig.findProjectContaining(candidate) - projectConfig?.path + def URI getProjectBaseDir(URI uri) { + val projectConfig = workspaceConfig.findProjectContaining(uri) + return projectConfig?.path } def ProjectManager getProjectManager(URI uri) { diff --git a/org.eclipse.xtext.ide/xtend-gen/org/eclipse/xtext/ide/server/WorkspaceManager.java b/org.eclipse.xtext.ide/xtend-gen/org/eclipse/xtext/ide/server/WorkspaceManager.java index 2654fe784..7cf39b4dc 100644 --- a/org.eclipse.xtext.ide/xtend-gen/org/eclipse/xtext/ide/server/WorkspaceManager.java +++ b/org.eclipse.xtext.ide/xtend-gen/org/eclipse/xtext/ide/server/WorkspaceManager.java @@ -21,6 +21,9 @@ import org.apache.log4j.Logger; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.lsp4j.TextEdit; +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException; +import org.eclipse.lsp4j.jsonrpc.messages.ResponseError; +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode; import org.eclipse.xtext.ide.server.BuildManager; import org.eclipse.xtext.ide.server.Document; import org.eclipse.xtext.ide.server.ILanguageServerAccess; @@ -68,7 +71,7 @@ public class WorkspaceManager { private Procedure2> issueAcceptor; - private IWorkspaceConfig workspaceConfig; + private IWorkspaceConfig _workspaceConfig; private List buildListeners = CollectionLiterals.newArrayList(); @@ -115,7 +118,7 @@ public class WorkspaceManager { } protected void refreshWorkspaceConfig(final CancelIndicator cancelIndicator) { - this.workspaceConfig = this.workspaceConfigFactory.getWorkspaceConfig(this.baseDir); + this.setWorkspaceConfig(this.workspaceConfigFactory.getWorkspaceConfig(this.baseDir)); final ArrayList newProjects = CollectionLiterals.newArrayList(); Set _keySet = this.projectName2ProjectManager.keySet(); final Set remainingProjectNames = new HashSet(_keySet); @@ -134,7 +137,7 @@ public class WorkspaceManager { newProjects.add(projectDescription); } }; - this.workspaceConfig.getProjects().forEach(_function); + this.getWorkspaceConfig().getProjects().forEach(_function); for (final String deletedProject : remainingProjectNames) { { this.projectName2ProjectManager.remove(deletedProject); @@ -145,6 +148,18 @@ public class WorkspaceManager { this.afterBuild(result); } + protected IWorkspaceConfig getWorkspaceConfig() { + if ((this._workspaceConfig == null)) { + final ResponseError error = new ResponseError(ResponseErrorCode.serverNotInitialized, "Workspace has not been initialized yet.", null); + throw new ResponseErrorException(error); + } + return this._workspaceConfig; + } + + protected void setWorkspaceConfig(final IWorkspaceConfig workspaceConfig) { + this._workspaceConfig = workspaceConfig; + } + protected void afterBuild(final List deltas) { for (final ILanguageServerAccess.IBuildListener listener : this.buildListeners) { listener.afterBuild(deltas); @@ -169,21 +184,17 @@ public class WorkspaceManager { return new ChunkedResourceDescriptions(this.fullIndex); } - public URI getProjectBaseDir(final URI candidate) { - URI _xblockexpression = null; - { - final IProjectConfig projectConfig = this.workspaceConfig.findProjectContaining(candidate); - URI _path = null; - if (projectConfig!=null) { - _path=projectConfig.getPath(); - } - _xblockexpression = _path; + public URI getProjectBaseDir(final URI uri) { + final IProjectConfig projectConfig = this.getWorkspaceConfig().findProjectContaining(uri); + URI _path = null; + if (projectConfig!=null) { + _path=projectConfig.getPath(); } - return _xblockexpression; + return _path; } public ProjectManager getProjectManager(final URI uri) { - final IProjectConfig projectConfig = this.workspaceConfig.findProjectContaining(uri); + final IProjectConfig projectConfig = this.getWorkspaceConfig().findProjectContaining(uri); String _name = null; if (projectConfig!=null) { _name=projectConfig.getName();