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>
* <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());
}
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 ---
* <EXPECTED CONTENT OF GENERATED D.TS FILE>
* // ---
* </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);
}
}
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 --> <FILE AND RANGE>
* </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());
}
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());
}
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;
}
Aggregations