Search in sources :

Example 16 with FileURI

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

the class TestWorkspaceManager method getFileURIFromModuleName.

/**
 * Translates a given module name to a file URI used in LSP call data. When 'moduleName' is <code>null</code>, the
 * file URI of the {@link #DEFAULT_MODULE_NAME default module} will be returned.
 * <p>
 * Because <code>package.json</code> files often play a similar role as modules (e.g. when asserting issues), this
 * method also supports <code>package.json</code> files, even though they aren't modules: for special names of the
 * format
 *
 * <pre>
 * &lt;project-name>/package.json
 * </pre>
 *
 * this method will return the file URI of the <code>package.json</code> file of the project with the given name.
 *
 * @throws IllegalStateException
 *             when no module or multiple modules are found for the given name, or some other error occurred.
 */
public FileURI getFileURIFromModuleName(String moduleName) {
    // special case for package.json files:
    if (moduleName != null && moduleName.endsWith("/" + N4JSGlobals.PACKAGE_JSON)) {
        String projectName = moduleName.substring(0, moduleName.length() - (1 + N4JSGlobals.PACKAGE_JSON.length()));
        File packageJsonFile = getPackageJsonFile(projectName);
        return new FileURI(packageJsonFile);
    } else if (moduleName != null && moduleName.contains(N4JSGlobals.PACKAGE_JSON)) {
        // in this case, people probably messed up the special syntax for denoting package.json files:
        Assert.fail("format for denoting package.json inside a module name argument is \"<project name>/" + N4JSGlobals.PACKAGE_JSON + "\", but argument was: \"" + moduleName + "\"");
    }
    // standard case for modules ('moduleName' seems to denote the name of a module):
    String extension = getN4JSNameAndExtension(moduleName).extension == null ? "." + DEFAULT_EXTENSION : "";
    String moduleNameWithExtension = getModuleNameOrDefault(moduleName) + extension;
    List<Path> allMatches;
    try {
        allMatches = Files.find(getRoot().toPath(), 99, (path, options) -> path.endsWith(moduleNameWithExtension)).collect(Collectors.toList());
    } catch (IOException e) {
        throw new IllegalStateException("Error when searching for module " + moduleNameWithExtension, e);
    }
    if (allMatches.isEmpty()) {
        throw new IllegalStateException("Module not found with name " + moduleNameWithExtension);
    }
    if (allMatches.size() > 1) {
        throw new IllegalStateException("Multiple modules found with name " + moduleNameWithExtension);
    }
    return new FileURI(allMatches.get(0).toFile());
}
Also used : Path(java.nio.file.Path) FileURI(org.eclipse.n4js.workspace.locations.FileURI) IOException(java.io.IOException) File(java.io.File)

Example 17 with FileURI

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

the class XtIdeTest method generated_dts.

/**
 * Compares the content of the generated .d.ts file with the expectation string.
 *
 * <pre>
 * // XPECT generated_dts ---
 * &lt;EXPECTED CONTENT OF GENERATED D.TS FILE&gt;
 * // ---
 * </pre>
 */
@Xpect
public void generated_dts(XtMethodData data) throws IOException {
    String moduleName = xtData.workspace.moduleNameOfXtFile;
    int idxStart = Math.max(moduleName.lastIndexOf("/") + 1, 0);
    int idxEnd = moduleName.lastIndexOf(".");
    String genDtsFileName = moduleName.substring(idxStart, idxEnd) + ".d.ts";
    try {
        FileURI genDtsFileURI = getFileURIFromModuleName(genDtsFileName);
        String genDtsCode = Files.readString(genDtsFileURI.toPath());
        assertTrue(genDtsCode.startsWith(OUTPUT_FILE_PREAMBLE));
        String genDtsCodeTrimmedPreamble = genDtsCode.substring(OUTPUT_FILE_PREAMBLE.length()).trim();
        assertTrue(genDtsCodeTrimmedPreamble.startsWith(IMPORT_N4JSGLOBALS));
        String genDtsCodeTrimmed = genDtsCodeTrimmedPreamble.substring(IMPORT_N4JSGLOBALS.length()).trim();
        assertEquals(data.getUnescapeExpectationRaw(), genDtsCodeTrimmed);
        CliTools cliTools = new CliTools();
        ensureTSC(cliTools);
        List<Project> allProjectsWithGenerateDts = FluentIterable.from(xtData.workspace.getAllProjects()).filter(Project::isGenerateDts).toList();
        assertFalse("no projects found with .d.ts generation turned on", allProjectsWithGenerateDts.isEmpty());
        for (Project project : allProjectsWithGenerateDts) {
            File workingDir = getProjectRoot(project.getName());
            // copy n4jsglobals.d.ts to output dir to make d.ts globals available
            Path n4jsGlobalsDTS = N4jsLibsAccess.getN4JSGlobalsDTS();
            Files.copy(n4jsGlobalsDTS, workingDir.toPath().resolve("src-gen/n4jsglobals.d.ts"));
            ProcessResult result;
            try {
                result = cliTools.nodejsRun(workingDir.toPath(), TSC2.getAbsoluteFile().toPath());
            } catch (CliException e) {
                throw new AssertionError("error while running tsc in working directory: " + workingDir, e);
            }
            assertFalse("TypeScript Error: " + result.getStdOut(), result.getStdOut().contains(": error "));
        }
    } catch (IllegalStateException e) {
        throw new RuntimeException("Could not find file " + genDtsFileName + "\nDid you set: " + XtSetupParser.GENERATE_DTS + " in SETUP section?", e);
    }
}
Also used : Path(java.nio.file.Path) ProcessResult(org.eclipse.n4js.cli.helper.ProcessResult) CliTools(org.eclipse.n4js.cli.helper.CliTools) Project(org.eclipse.n4js.tests.codegen.Project) FileURI(org.eclipse.n4js.workspace.locations.FileURI) CliException(org.eclipse.n4js.cli.helper.CliTools.CliException) File(java.io.File) Xpect(org.eclipse.xpect.runner.Xpect)

Example 18 with FileURI

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

the class XtIdeTest method definition.

/**
 * Calls LSP endpoint 'definition'.
 *
 * <pre>
 * // Xpect definition --&gt; &ltFILE AND RANGE&gt
 * </pre>
 */
// NOTE: This annotation is used only to enable validation and navigation of .xt files.
@Xpect
public void definition(XtMethodData data) throws InterruptedException, ExecutionException {
    Position position = eobjProvider.checkAndGetPosition(data, "definition", "at");
    FileURI uri = getFileURIFromModuleName(xtData.workspace.moduleNameOfXtFile);
    CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> future = callDefinition(uri.toString(), position.getLine(), position.getCharacter());
    Either<List<? extends Location>, List<? extends LocationLink>> definitions = future.get();
    String actualSignatureHelp = getStringLSP4J().toString4(definitions);
    assertEquals(data.expectation, actualSignatureHelp.trim());
}
Also used : LocationLink(org.eclipse.lsp4j.LocationLink) FileURI(org.eclipse.n4js.workspace.locations.FileURI) Position(org.eclipse.lsp4j.Position) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) List(java.util.List) ArrayList(java.util.ArrayList) CompletionList(org.eclipse.lsp4j.CompletionList) Location(org.eclipse.lsp4j.Location) Xpect(org.eclipse.xpect.runner.Xpect)

Example 19 with FileURI

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

the class MockWorkspaceSupplier method createProjectConfig.

/**
 * See {@link #createWorkspaceConfig()}.
 */
protected N4JSProjectConfigSnapshot createProjectConfig() {
    // try to load a test package.json from disk, otherwise create a project configuration from default values
    Pair<FileURI, ProjectDescription> loadedOrCreated = loadProjectDescription().or(this::createProjectDescription);
    FileURI projectPath = loadedOrCreated.getKey();
    ProjectDescription pd = loadedOrCreated.getValue();
    List<N4JSSourceFolderSnapshot> sourceFolders = createSourceFolders(projectPath, pd);
    return new N4JSProjectConfigSnapshot(pd, projectPath.withTrailingPathDelimiter().toURI(), false, true, Collections.emptyList(), sourceFolders, Map.of());
}
Also used : FileURI(org.eclipse.n4js.workspace.locations.FileURI) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)

Example 20 with FileURI

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

the class AccessControlTest method compile.

/**
 * Compiles the projects generated into the path at {@link #FIXTURE_ROOT}, which in this test case the projects
 * representing the currently tested scenario and returns the generated issues.
 *
 * @return the generated issues
 */
private static Collection<Issue> compile() {
    IdeTestLanguageClient languageClient = testLspManager.getLanguageClient();
    languageClient.clearIssues();
    testLspManager.cleanBuildAndWait();
    IssueToDiagnosticConverter converter = new IssueToDiagnosticConverter();
    List<Issue> issues = new ArrayList<>();
    for (Map.Entry<FileURI, Diagnostic> uriDiagnostic : languageClient.getIssues().entries()) {
        FileURI uri = uriDiagnostic.getKey();
        Diagnostic diagnostic = uriDiagnostic.getValue();
        issues.add(converter.toIssue(uri.toURI(), diagnostic, Optional.absent()));
    }
    return issues;
}
Also used : IdeTestLanguageClient(org.eclipse.n4js.ide.tests.helper.client.IdeTestLanguageClient) FileURI(org.eclipse.n4js.workspace.locations.FileURI) Issue(org.eclipse.xtext.validation.Issue) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) IssueToDiagnosticConverter(org.eclipse.n4js.xtext.ide.server.issues.IssueToDiagnosticConverter) HashMap(java.util.HashMap) Map(java.util.Map)

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