Search in sources :

Example 41 with FileURI

use of org.eclipse.n4js.workspace.locations.FileURI in project n4js by eclipse.

the class AbstractIdeTest method deleteFolderNotContainingOpenFiles.

/**
 * Delete a folder and its entire content and notify the LSP server. The folder must not contain open files.
 * <p>
 * Which URIs the LSP client will include in the resulting <code>didChangeWatchedFiles</code> notification depends
 * on the configuration during server start-up and initialization. For example, a VSCode extension might configure a
 * static glob pattern for this purpose or the server could dynamically register for file events in certain folders.
 * To simulate various such configurations in tests, this method provides the <code>nameRegEx</code> parameter.
 *
 * @param folderURI
 *            URI of the folder to be deleted. Must not contain open files.
 * @param nameRegEx
 *            the URI of a deleted file/folder is included in the <code>didChangeWatchedFiles</code> notification if
 *            and only if the file/folder's name matches this regular expression.
 */
protected void deleteFolderNotContainingOpenFiles(FileURI folderURI, String nameRegEx) {
    // 1) collect all files and folders to be deleted
    List<FileURI> allFoldersAndFiles;
    try (Stream<Path> walker = Files.walk(folderURI.toFile().toPath())) {
        allFoldersAndFiles = walker.map(p -> toFileURI(p.toFile())).collect(Collectors.toList());
    } catch (IOException e) {
        throw new WrappedException("exception while walking file tree", e);
    }
    // 2) delete on disk
    for (FileURI currURI : Lists.reverse(allFoldersAndFiles)) {
        deleteFileOnDiskWithoutNotification(currURI);
    }
    Assert.assertFalse("folder was not properly deleted on disk", folderURI.toFile().exists());
    // 3) notify the LSP server
    Pattern pattern = Pattern.compile(nameRegEx);
    List<FileEvent> fileEvents = allFoldersAndFiles.stream().filter(uri -> pattern.matcher(uri.getName()).matches()).map(uri -> new FileEvent(uri.toString(), FileChangeType.Deleted)).collect(Collectors.toList());
    Assert.assertFalse("at least one deleted file/folder must match the 'nameRegEx'", fileEvents.isEmpty());
    DidChangeWatchedFilesParams params = new DidChangeWatchedFilesParams(fileEvents);
    languageServer.didChangeWatchedFiles(params);
}
Also used : Path(java.nio.file.Path) DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) CliTools(org.eclipse.n4js.cli.helper.CliTools) FileTime(java.nio.file.attribute.FileTime) FileUtils(org.eclipse.n4js.utils.io.FileUtils) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) UriExtensions(org.eclipse.xtext.ide.server.UriExtensions) Collections.singletonList(java.util.Collections.singletonList) DidSaveTextDocumentParams(org.eclipse.lsp4j.DidSaveTextDocumentParams) FluentIterable(com.google.common.collect.FluentIterable) N4JSLanguageConstants(org.eclipse.n4js.N4JSLanguageConstants) Optional(com.google.common.base.Optional) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) Map(java.util.Map) Path(java.nio.file.Path) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) AfterClass(org.junit.AfterClass) Set(java.util.Set) NameAndExtension(org.eclipse.n4js.ide.tests.helper.server.TestWorkspaceManager.NameAndExtension) WrappedException(org.eclipse.emf.common.util.WrappedException) LocationLink(org.eclipse.lsp4j.LocationLink) SystemOutRedirecter(org.eclipse.n4js.cli.helper.SystemOutRedirecter) ConcurrentIndex(org.eclipse.n4js.xtext.ide.server.build.ConcurrentIndex) Stream(java.util.stream.Stream) Assert.assertFalse(org.junit.Assert.assertFalse) Pair(org.eclipse.xtext.xbase.lib.Pair) GlobalRegistries(org.eclipse.xtext.testing.GlobalRegistries) Joiner(com.google.common.base.Joiner) Iterables(com.google.common.collect.Iterables) URI(org.eclipse.emf.common.util.URI) ApplyWorkspaceEditParams(org.eclipse.lsp4j.ApplyWorkspaceEditParams) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IIdeTestLanguageClientListener(org.eclipse.n4js.ide.tests.helper.client.IdeTestLanguageClient.IIdeTestLanguageClientListener) Diagnostic(org.eclipse.lsp4j.Diagnostic) ArrayList(java.util.ArrayList) Multimaps(com.google.common.collect.Multimaps) Strings(com.google.common.base.Strings) FileChangeType(org.eclipse.lsp4j.FileChangeType) TextDocumentItem(org.eclipse.lsp4j.TextDocumentItem) BuilderFrontend(org.eclipse.n4js.xtext.ide.server.build.BuilderFrontend) TextEdit(org.eclipse.lsp4j.TextEdit) Lists(com.google.common.collect.Lists) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) LanguageInfo(org.eclipse.xtext.LanguageInfo) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) LinkedHashSet(java.util.LinkedHashSet) Before(org.junit.Before) Files(java.nio.file.Files) N4JSLanguageUtils(org.eclipse.n4js.utils.N4JSLanguageUtils) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams) IOException(java.io.IOException) GlobalStateMemento(org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) File(java.io.File) SourceFolderSnapshot(org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot) Assert.assertNull(org.junit.Assert.assertNull) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) Module(com.google.inject.Module) ProcessResult(org.eclipse.n4js.cli.helper.ProcessResult) BiFunction(java.util.function.BiFunction) Inject(com.google.inject.Inject) MessageType(org.eclipse.lsp4j.MessageType) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) FileEvent(org.eclipse.lsp4j.FileEvent) After(org.junit.After) Location(org.eclipse.lsp4j.Location) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Assert.fail(org.junit.Assert.fail) DidCloseTextDocumentParams(org.eclipse.lsp4j.DidCloseTextDocumentParams) FileURI(org.eclipse.n4js.workspace.locations.FileURI) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) StandardOpenOption(java.nio.file.StandardOpenOption) ProjectConfigSnapshot(org.eclipse.n4js.xtext.workspace.ProjectConfigSnapshot) ResourceDescriptionsData(org.eclipse.xtext.resource.impl.ResourceDescriptionsData) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) DefinitionParams(org.eclipse.lsp4j.DefinitionParams) Objects(java.util.Objects) CompletionItem(org.eclipse.lsp4j.CompletionItem) List(java.util.List) DidOpenTextDocumentParams(org.eclipse.lsp4j.DidOpenTextDocumentParams) N4jsLibsAccess(org.eclipse.n4js.cli.helper.N4jsLibsAccess) Entry(java.util.Map.Entry) Pattern(java.util.regex.Pattern) BuildOrderFactory(org.eclipse.n4js.xtext.workspace.BuildOrderFactory) CompletionParams(org.eclipse.lsp4j.CompletionParams) BeforeClass(org.junit.BeforeClass) XLanguageServerImpl(org.eclipse.n4js.xtext.ide.server.XLanguageServerImpl) BuildOrderIterator(org.eclipse.n4js.xtext.workspace.BuildOrderIterator) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Range(org.eclipse.lsp4j.Range) Multimap(com.google.common.collect.Multimap) Function(java.util.function.Function) XDocument(org.eclipse.n4js.xtext.ide.server.XDocument) N4JSGlobals(org.eclipse.n4js.N4JSGlobals) MessageParams(org.eclipse.lsp4j.MessageParams) ResourceChange(org.eclipse.lsp4j.ResourceChange) Position(org.eclipse.lsp4j.Position) CompletionList(org.eclipse.lsp4j.CompletionList) IdeTestLanguageClient(org.eclipse.n4js.ide.tests.helper.client.IdeTestLanguageClient) PrintStream(java.io.PrintStream) Assert.assertNotNull(org.junit.Assert.assertNotNull) SetView(com.google.common.collect.Sets.SetView) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) WorkspaceConfigSnapshot(org.eclipse.n4js.xtext.workspace.WorkspaceConfigSnapshot) IterableExtensions(org.eclipse.xtext.xbase.lib.IterableExtensions) Comparator(java.util.Comparator) Collections(java.util.Collections) Pattern(java.util.regex.Pattern) FileEvent(org.eclipse.lsp4j.FileEvent) FileURI(org.eclipse.n4js.workspace.locations.FileURI) WrappedException(org.eclipse.emf.common.util.WrappedException) IOException(java.io.IOException) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams)

Example 42 with FileURI

use of org.eclipse.n4js.workspace.locations.FileURI in project n4js by eclipse.

the class AbstractRenameTest method applyWorkspaceEdit.

/**
 * Unchanged modules are not included in the returned map.
 */
private Map<FileURI, String> applyWorkspaceEdit(Map<String, Map<String, String>> projectsModulesSourcesBefore, WorkspaceEdit edit, RenamePosition pos, RenameTestConfiguration config) {
    Map<FileURI, List<TextEdit>> fileURI2TextEdits = new LinkedHashMap<>();
    for (Entry<String, List<TextEdit>> entry : edit.getChanges().entrySet()) {
        String uriStr = entry.getKey();
        List<TextEdit> textEdits = entry.getValue();
        FileURI fileURI = getFileURIFromURIString(uriStr);
        fileURI2TextEdits.put(fileURI, textEdits);
    }
    Map<FileURI, String> fileURI2ActualSourceAfter = new LinkedHashMap<>();
    for (Entry<String, Map<String, String>> entry1 : projectsModulesSourcesBefore.entrySet()) {
        String projectName = entry1.getKey();
        Map<String, String> moduleName2SourceBefore = entry1.getValue();
        if (projectName.startsWith("#")) {
            // ignore entries with special information, e.g. TestWorkspaceManager#NODE_MODULES
            continue;
        }
        for (Entry<String, String> entry2 : moduleName2SourceBefore.entrySet()) {
            String moduleName = entry2.getKey();
            String sourceBefore = entry2.getValue();
            if (moduleName.startsWith("#")) {
                // ignore entries with special information, e.g. TestWorkspaceManager#DEPENDENCIES
                continue;
            }
            FileURI fileURI = getFileURIFromModuleName(moduleName);
            List<TextEdit> textEdits = fileURI2TextEdits.get(fileURI);
            if (textEdits != null) {
                String actualSourceAfter = applyTextEdits(sourceBefore, textEdits);
                fileURI2ActualSourceAfter.put(fileURI, actualSourceAfter);
            } else {
            // no changes in this file -> ignore
            }
        }
    }
    Set<FileURI> unknownURIs = new LinkedHashSet<>(fileURI2TextEdits.keySet());
    unknownURIs.removeAll(fileURI2ActualSourceAfter.keySet());
    if (!unknownURIs.isEmpty()) {
        String sourceBefore = config.projectsModulesSourcesBefore.get(pos.projectName).get(pos.moduleName);
        fail("rename led to text edits in unknown URIs: " + Joiner.on(", ").join(unknownURIs), sourceBefore, pos);
    }
    return fileURI2ActualSourceAfter;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LinkedHashMap(java.util.LinkedHashMap) FileURI(org.eclipse.n4js.workspace.locations.FileURI) TextEdit(org.eclipse.lsp4j.TextEdit) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 43 with FileURI

use of org.eclipse.n4js.workspace.locations.FileURI in project n4js by eclipse.

the class TestLspManager method doStartLspServer.

/**
 * Connects, initializes and waits for the initial build of the test project.
 */
@SuppressWarnings("deprecation")
protected void doStartLspServer(File root) {
    ClientCapabilities capabilities = new ClientCapabilities();
    WorkspaceClientCapabilities wcc = new WorkspaceClientCapabilities();
    wcc.setExecuteCommand(new ExecuteCommandCapabilities());
    capabilities.setWorkspace(wcc);
    TextDocumentClientCapabilities tdcc = new TextDocumentClientCapabilities();
    // activate 'prepareRename' requests
    tdcc.setRename(new RenameCapabilities(true, false));
    capabilities.setTextDocument(tdcc);
    InitializeParams initParams = new InitializeParams();
    initParams.setCapabilities(capabilities);
    initParams.setRootUri(new FileURI(root).toString());
    languageServer.connect(languageClient);
    languageServer.initialize(initParams).join();
    languageServer.initialized(null);
}
Also used : WorkspaceClientCapabilities(org.eclipse.lsp4j.WorkspaceClientCapabilities) FileURI(org.eclipse.n4js.workspace.locations.FileURI) RenameCapabilities(org.eclipse.lsp4j.RenameCapabilities) WorkspaceClientCapabilities(org.eclipse.lsp4j.WorkspaceClientCapabilities) TextDocumentClientCapabilities(org.eclipse.lsp4j.TextDocumentClientCapabilities) ClientCapabilities(org.eclipse.lsp4j.ClientCapabilities) InitializeParams(org.eclipse.lsp4j.InitializeParams) ExecuteCommandCapabilities(org.eclipse.lsp4j.ExecuteCommandCapabilities) TextDocumentClientCapabilities(org.eclipse.lsp4j.TextDocumentClientCapabilities)

Example 44 with FileURI

use of org.eclipse.n4js.workspace.locations.FileURI in project n4js by eclipse.

the class XtIdeTest method output.

/**
 * Compiles and executes the current xt file and compares the output to the expected output
 *
 * <pre>
 * // Xpect output --&gt; &ltOUTPUT&gt
 * </pre>
 */
// NOTE: This annotation is used only to enable validation and navigation of .xt files.
@Xpect
public void output(XtMethodData data) {
    String moduleName = xtData.workspace.moduleNameOfXtFile;
    int idxStart = Math.max(moduleName.lastIndexOf("/") + 1, 0);
    int idxEnd = moduleName.lastIndexOf(".");
    String genModuleName = moduleName.substring(idxStart, idxEnd) + ".js";
    FileURI fileUri = getFileURIFromModuleName(genModuleName);
    installN4JSRuntime();
    assertOutput(fileUri, data.expectation);
}
Also used : FileURI(org.eclipse.n4js.workspace.locations.FileURI) Xpect(org.eclipse.xpect.runner.Xpect)

Example 45 with FileURI

use of org.eclipse.n4js.workspace.locations.FileURI in project n4js by eclipse.

the class XtIdeTest method initializeXtFile.

/**
 * Call this before calling any other methods of {@link XtIdeTest}.
 */
public void initializeXtFile(Set<String> globallySuppressedIssues, XtFileData newXtData) {
    Preconditions.checkNotNull(newXtData);
    xtData = newXtData;
    testWorkspaceManager.createTestOnDisk(xtData.workspace);
    Set<String> actuallySuppressedIssues = new HashSet<>(globallySuppressedIssues);
    actuallySuppressedIssues.removeAll(xtData.enabledIssues);
    actuallySuppressedIssues.addAll(xtData.disabledIssues);
    setSuppressedIssues(actuallySuppressedIssues);
    for (XtMethodData startupMethod : xtData.startupMethodData) {
        switch(startupMethod.name) {
            case "startAndWaitForLspServer":
                startAndWaitForLspServer();
                break;
            default:
                throw new IllegalArgumentException("Unknown method: " + startupMethod.name);
        }
    }
    FileURI xtModule = getFileURIFromModuleName(xtData.workspace.moduleNameOfXtFile);
    languageServer.getResourceTaskManager().runInTemporaryContext(xtModule.toURI(), "test", false, (context, ci) -> {
        resource = context.getResource();
        Preconditions.checkNotNull(resource);
        eobjProvider = new XtResourceEObjectAccessor(xtData, resource);
        if (resource instanceof N4JSResource) {
            N4JSResource n4Res = ((N4JSResource) resource);
            n4Res.resolveLazyCrossReferences(ci);
        }
        return null;
    }).join();
    ArrayList<XtMethodData> issueTests = new ArrayList<>();
    LOOP: for (XtMethodData testMethod : xtData.getTestMethodData()) {
        switch(testMethod.name) {
            case "nowarnings":
            case "noerrors":
            case "warnings":
            case "errors":
                issueTests.add(testMethod);
                break;
            default:
                // test method data is sorted: issue related tests methods come first
                break LOOP;
        }
    }
    this.issueHelper = new XtMethodsIssues(xtData, getIssuesInFile(xtModule), issueTests);
}
Also used : ProcessResult(org.eclipse.n4js.cli.helper.ProcessResult) Inject(com.google.inject.Inject) CliTools(org.eclipse.n4js.cli.helper.CliTools) AbstractIdeTest(org.eclipse.n4js.ide.tests.helper.server.AbstractIdeTest) TraverseDirection(org.eclipse.n4js.flowgraphs.analysis.TraverseDirection) FluentIterable(com.google.common.collect.FluentIterable) Location(org.eclipse.lsp4j.Location) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) CliException(org.eclipse.n4js.cli.helper.CliTools.CliException) Path(java.nio.file.Path) FileURI(org.eclipse.n4js.workspace.locations.FileURI) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IMPORT_N4JSGLOBALS(org.eclipse.n4js.N4JSGlobals.IMPORT_N4JSGLOBALS) Set(java.util.Set) EObject(org.eclipse.emf.ecore.EObject) LocationLink(org.eclipse.lsp4j.LocationLink) Sets(com.google.common.collect.Sets) CompletionItem(org.eclipse.lsp4j.CompletionItem) Match(org.eclipse.n4js.ide.tests.helper.server.xt.XtMethodPattern.Match) Strings(org.eclipse.n4js.utils.Strings) List(java.util.List) N4jsLibsAccess(org.eclipse.n4js.cli.helper.N4jsLibsAccess) Assert.assertFalse(org.junit.Assert.assertFalse) ControlFlowType(org.eclipse.n4js.flowgraphs.ControlFlowType) Xpect(org.eclipse.xpect.runner.Xpect) Iterables(com.google.common.collect.Iterables) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) N4JSGlobals(org.eclipse.n4js.N4JSGlobals) ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) Position(org.eclipse.lsp4j.Position) CompletionList(org.eclipse.lsp4j.CompletionList) XtextResource(org.eclipse.xtext.resource.XtextResource) Iterator(java.util.Iterator) Files(java.nio.file.Files) Assert.assertTrue(org.junit.Assert.assertTrue) TMember(org.eclipse.n4js.ts.types.TMember) IOException(java.io.IOException) Project(org.eclipse.n4js.tests.codegen.Project) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) N4JSResource(org.eclipse.n4js.resource.N4JSResource) OUTPUT_FILE_PREAMBLE(org.eclipse.n4js.N4JSGlobals.OUTPUT_FILE_PREAMBLE) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) FileURI(org.eclipse.n4js.workspace.locations.FileURI) N4JSResource(org.eclipse.n4js.resource.N4JSResource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

FileURI (org.eclipse.n4js.workspace.locations.FileURI)49 List (java.util.List)13 Path (java.nio.file.Path)11 ArrayList (java.util.ArrayList)11 File (java.io.File)10 Position (org.eclipse.lsp4j.Position)9 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)9 IOException (java.io.IOException)7 CompletionList (org.eclipse.lsp4j.CompletionList)7 Collections (java.util.Collections)6 Range (org.eclipse.lsp4j.Range)6 Lists (com.google.common.collect.Lists)5 Sets (com.google.common.collect.Sets)5 Files (java.nio.file.Files)5 LinkedHashSet (java.util.LinkedHashSet)5 Set (java.util.Set)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 URI (org.eclipse.emf.common.util.URI)5 CompletionItem (org.eclipse.lsp4j.CompletionItem)5 Diagnostic (org.eclipse.lsp4j.Diagnostic)5