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;
}
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;
}
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;
}
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);
}
}
}
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());
}
}
Aggregations