Search in sources :

Example 26 with FileURI

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

the class NodeModulesDiscoveryHelper method getWorkspacesOfYarnWorkspaceProject.

private List<String> getWorkspacesOfYarnWorkspaceProject(File yarnProjectFolder, Map<Path, ProjectDescription> pdCache) {
    // obtain value of property "workspaces" in package.json located in folder 'candidate'
    final ProjectDescription prjDescr = pdCache.computeIfAbsent(yarnProjectFolder.toPath(), // load value from package.json
    p -> {
        FileURI location = new FileURI(yarnProjectFolder);
        // yarn projects do not have a related root, hence location is given
        ProjectDescription pd = projectDescriptionLoader.loadProjectDescriptionAtLocation(location, null);
        return pd;
    });
    final List<String> workspaces = (prjDescr != null && prjDescr.isYarnWorkspaceRoot()) ? prjDescr.getWorkspaces() : null;
    return workspaces;
}
Also used : FileURI(org.eclipse.n4js.workspace.locations.FileURI) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)

Example 27 with FileURI

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

the class ProjectDiscoveryHelper method getOrCreateProjectDescription.

/**
 * @return the potentially cached {@link ProjectDescription} for the project in the given directory or creates it.
 *         In case creation fails, null is returned.
 */
private ProjectDescription getOrCreateProjectDescription(Path path, Path relatedRoot, Map<Path, ProjectDescription> cache) {
    if (!cache.containsKey(path) || cache.get(path).getRelatedRootLocation() == null) {
        FileURI location = new FileURI(path.toFile());
        FileURI relatedRootLocation = relatedRoot == null ? null : new FileURI(relatedRoot.toFile());
        ProjectDescription pd = projectDescriptionLoader.loadProjectDescriptionAtLocation(location, relatedRootLocation);
        cache.put(path, pd);
    }
    ProjectDescription pd = cache.get(path);
    if (pd != null && relatedRoot != null && pd.getRelatedRootLocation() != null && !Objects.equals(relatedRoot, pd.getRelatedRootLocation().toPath())) {
        // recompute project description in case the nodeModulesDiscoveryHelper added it
        ProjectDescriptionBuilder pdb = pd.change();
        pd = pdb.setRelatedRootLocation(new FileURI(relatedRoot.toFile())).setId(pdb.computeProjectID()).build();
        cache.put(path, pd);
    }
    return pd;
}
Also used : FileURI(org.eclipse.n4js.workspace.locations.FileURI) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription) ProjectDescriptionBuilder(org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder)

Example 28 with FileURI

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

the class PackageJsonValidatorExtension method holdsExistingDirectoryPath.

/**
 * Checks whether the given {@code pathLiteral} represents an existing relative path to the currently validated
 * {@link Resource}.
 *
 * Returns {@code false} and adds issues to {@code pathLiteral} otherwise.
 */
private boolean holdsExistingDirectoryPath(JSONStringLiteral pathLiteral) {
    final Resource resource = pathLiteral.eResource();
    final URI resourceURI = resource.getURI();
    final N4JSProjectConfigSnapshot n4jsProject = workspaceAccess.findProjectContaining(resource);
    if (n4jsProject == null) {
        // container project cannot be determined, fail gracefully (validation running on non-N4JS project?)
        return true;
    }
    final FileURI path = n4jsProject.getPathAsFileURI();
    final URI projectLocation = path.toURI();
    // resolve against project uri with trailing slash
    final URI projectRelativeResourceURI = resourceURI.deresolve(projectLocation.appendSegment(""));
    final Path absoluteProjectPath = path.toFileSystemPath();
    if (absoluteProjectPath == null) {
        throw new IllegalStateException("Failed to compute project path for package.json at " + resourceURI.toString());
    }
    // compute the path of the folder that contains the currently validated package.json file
    final Path baseResourcePath = new File(absoluteProjectPath.toFile(), projectRelativeResourceURI.trimSegments(1).toFileString()).toPath();
    final String relativePath = pathLiteral.getValue();
    final File file = new File(baseResourcePath.toString(), relativePath);
    if (!file.exists()) {
        addIssue(IssueCodes.getMessageForPKGJ_NON_EXISTING_SOURCE_PATH(relativePath), pathLiteral, IssueCodes.PKGJ_NON_EXISTING_SOURCE_PATH);
        return false;
    }
    if (!file.isDirectory()) {
        addIssue(IssueCodes.getMessageForPKGJ_EXPECTED_DIRECTORY_PATH(relativePath), pathLiteral, IssueCodes.PKGJ_EXPECTED_DIRECTORY_PATH);
        return false;
    }
    return true;
}
Also used : Path(java.nio.file.Path) FileURI(org.eclipse.n4js.workspace.locations.FileURI) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) Resource(org.eclipse.emf.ecore.resource.Resource) FileURI(org.eclipse.n4js.workspace.locations.FileURI) URI(org.eclipse.emf.common.util.URI) File(java.io.File)

Example 29 with FileURI

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

the class AbstractCompletionTest method performTest.

@Override
protected void performTest(Project project, String moduleName, N4JSTestCompletionConfiguration tcc) throws InterruptedException, ExecutionException {
    FileURI uri = getFileURIFromModuleName(tcc.getFilePath());
    CompletableFuture<Either<List<CompletionItem>, CompletionList>> future = callCompletion(uri.toString(), tcc.getLine(), tcc.getColumn());
    Either<List<CompletionItem>, CompletionList> result = future.get();
    List<CompletionItem> items = result.isLeft() ? result.getLeft() : result.getRight().getItems();
    // assert already sorted
    List<CompletionItem> sortedItems = ListExtensions.sortInplaceBy(items, CompletionItem::getSortText);
    Assert.assertEquals(items, sortedItems);
    if (tcc.getAssertCompletionList() != null) {
        if (tcc.getExpectedCodeAfterApply() != null) {
            Assert.fail("assertCompletionList must not be combined with expectedCodeAfterApply");
        }
        tcc.getAssertCompletionList().apply(result.getRight());
    } else {
        String actualItemsStr = Strings.join("\n", getStringLSP4J()::toString, items);
        String expectedItemsStr = tcc.getExpectedCompletionItems();
        String expectedItemsStrNoApplyMarker = expectedItemsStr.replaceFirst("(^|\\n)\\s*" + Pattern.quote(APPLY), "");
        assertEquals(expectedItemsStrNoApplyMarker.trim(), actualItemsStr.trim());
        String expectedCodeAfterApply = tcc.getExpectedCodeAfterApply();
        CompletionItem itemToBeApplied = getCompletionItemToBeApplied(sortedItems, tcc);
        if (expectedCodeAfterApply != null && itemToBeApplied != null) {
            Position pos = new Position(tcc.getLine(), tcc.getColumn());
            String actualCodeAfterApply = applyCompletionItem(tcc.getModel(), pos, itemToBeApplied);
            String msg = "application of completion item did not yield correct result\n" + "EXPECTED:\n" + expectedCodeAfterApply + "\n" + "ACTUAL:\n" + actualCodeAfterApply;
            Assert.assertEquals(msg, expectedCodeAfterApply.trim(), actualCodeAfterApply.trim());
        } else if (expectedCodeAfterApply == null && itemToBeApplied != null) {
            Assert.fail("a completion item was marked for application with " + APPLY + ", but no expected code after application was given");
        } else if (expectedCodeAfterApply != null && itemToBeApplied == null) {
            Assert.fail("expected code after application was given " + "but no completion item was marked for application with " + APPLY);
        }
    }
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) FileURI(org.eclipse.n4js.workspace.locations.FileURI) CompletionItem(org.eclipse.lsp4j.CompletionItem) Position(org.eclipse.lsp4j.Position) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList)

Example 30 with FileURI

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

the class AbstractCodeActionTest method performTest.

@Override
protected void performTest(Project project, String moduleName, N4JSTestCodeActionConfiguration tcac) throws InterruptedException, ExecutionException {
    CodeActionParams codeActionParams = new CodeActionParams();
    Range range = new Range();
    Position posStart = new Position(tcac.getLine(), tcac.getColumn());
    Position posEnd = tcac.getEndLine() >= 0 && tcac.getEndColumn() >= 0 ? new Position(tcac.getEndLine(), tcac.getEndColumn()) : posStart;
    range.setStart(posStart);
    range.setEnd(posEnd);
    codeActionParams.setRange(range);
    CodeActionContext context = new CodeActionContext();
    FileURI uri = getFileURIFromModuleName(moduleName);
    context.setDiagnostics(Lists.newArrayList(getIssuesInFile(uri)));
    codeActionParams.setContext(context);
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
    textDocument.setUri(uri.toString());
    codeActionParams.setTextDocument(textDocument);
    CompletableFuture<List<Either<Command, CodeAction>>> future = languageServer.codeAction(codeActionParams);
    List<Either<Command, CodeAction>> result = future.get();
    if (tcac.getAssertCodeActions() != null) {
        tcac.getAssertCodeActions().apply(result);
    } else {
        String resultStr = result.stream().map(cmdOrAction -> getStringLSP4J().toString3(cmdOrAction)).collect(Collectors.joining("\n-----\n"));
        assertEquals(tcac.getExpectedCodeActions().trim(), resultStr.trim());
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) CodeAction(org.eclipse.lsp4j.CodeAction) FileURI(org.eclipse.n4js.workspace.locations.FileURI) N4JSTestCodeActionConfiguration(org.eclipse.n4js.ide.tests.helper.server.AbstractCodeActionTest.N4JSTestCodeActionConfiguration) TestCodeActionConfiguration(org.eclipse.xtext.testing.AbstractLanguageServerTest.TestCodeActionConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) Range(org.eclipse.lsp4j.Range) Project(org.eclipse.n4js.tests.codegen.Project) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Collectors(java.util.stream.Collectors) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) ExecutionException(java.util.concurrent.ExecutionException) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) List(java.util.List) Lists(com.google.common.collect.Lists) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) Assert.assertEquals(org.junit.Assert.assertEquals) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) Range(org.eclipse.lsp4j.Range) FileURI(org.eclipse.n4js.workspace.locations.FileURI) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) List(java.util.List)

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