use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.
the class EditorContentExtractor method getDescriptorForSemanticElement.
/**
* Optionally returns with the semantic AST node element (given as the element URI) as a {@link StyledTextDescriptor
* styled text descriptor}. If the element cannot be resolved or the styled text cannot be computed this method
* returns with and {@link Optional#absent() absent} instance but never {@code null}.
*
* @param uri
* the URI of the semantic element in the AST.
* @return a styled text descriptor representing the extracted code for the semantic AST node given with its unique
* URI.
*/
public Optional<StyledTextDescriptor> getDescriptorForSemanticElement(final URI uri) {
if (null == uri) {
return absent();
}
final URI trimmedUri = uri.hasFragment() ? uri.trimFragment() : uri;
final IN4JSProject project = core.findProject(trimmedUri).orNull();
if (project == null) {
return absent();
}
final ResourceSet resSet = core.createResourceSet(Optional.of(project));
final IResourceDescriptions index = core.getXtextIndex(resSet);
final IResourceDescription resDesc = index.getResourceDescription(trimmedUri);
if (null == resDesc) {
return absent();
}
final TModule module = core.loadModuleFromIndex(resSet, resDesc, false);
if (null == module || null == module.eResource() || null == module.eResource().getResourceSet()) {
return absent();
}
final URI moduleUri = module.eResource().getURI();
final IFile file = getWorkspace().getRoot().getFile(new Path(moduleUri.toPlatformString(true)));
if (null == file || !file.exists()) {
return absent();
}
final FileEditorInput editorInput = new FileEditorInput(file);
try {
docProvider.connect(editorInput);
} catch (final CoreException e) {
LOGGER.error("Error while connecting editor input with document provider: " + e);
return absent();
}
final IDocument doc = docProvider.getDocument(editorInput);
if (null == doc) {
return absent();
}
final XtextResource xtextResource = (XtextResource) module.eResource();
final ResourceSet resourceSet = xtextResource.getResourceSet();
final EObject object = resourceSet.getEObject(uri, true);
if (null == object) {
return absent();
}
final ITextRegion textRegion = locationInFileProvider.getFullTextRegion(object);
if (null == textRegion) {
return absent();
}
try {
final int lineOfOffset = doc.getLineOfOffset(textRegion.getOffset());
final int lineOffset = doc.getLineOffset(lineOfOffset);
final int offset = lineOffset;
final int length = textRegion.getLength() + (textRegion.getOffset() - lineOffset);
final String text = doc.get(offset, length);
final IPresentationRepairer repairer = repairerProvider.get();
final IPresentationDamager damager = damagerProvider.get();
for (final String contentType : partitionTypeMapper.getSupportedPartitionTypes()) {
reconciler.setRepairer(repairer, contentType);
repairer.setDocument(doc);
reconciler.setDamager(damager, contentType);
damager.setDocument(doc);
}
final Region region = new Region(offset, length);
final TextPresentation textPresentation = reconciler.createRepairDescription(region, doc);
final Iterator<?> rangeItr = textPresentation.getAllStyleRangeIterator();
final Collection<StyleRange> ranges = newLinkedList();
while (rangeItr.hasNext()) {
final Object next = rangeItr.next();
if (next instanceof StyleRange) {
ranges.add((StyleRange) next);
}
}
final Range<Integer> textRange = Range.closed(offset, offset + length);
for (final Iterator<StyleRange> itr = ranges.iterator(); itr.hasNext(); ) /* nothing */
{
final StyleRange range = itr.next();
if (!textRange.contains(range.start) || !textRange.contains(range.start + range.length)) {
itr.remove();
} else {
range.start = range.start - offset;
}
}
return fromNullable(new StyledTextDescriptorImpl(text, ranges));
} catch (final BadLocationException e) {
LOGGER.error("Error while trying to extract text from document.", e);
return absent();
}
}
use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.
the class BuildOrderComputer method getBuildOrder.
/**
* Convenience method for {@link #getBuildOrder(N4JSProject[])}
*/
public VertexOrder<IN4JSProject> getBuildOrder(N4JSExternalProject[] projects) {
N4JSProject[] n4jsPrjs = new N4JSProject[projects.length];
for (int i = 0; i < projects.length; i++) {
N4JSExternalProject project = projects[i];
URI locationURI = URIUtils.convert(project);
N4JSProject prjDependency = (N4JSProject) core.findProject(locationURI).orNull();
n4jsPrjs[i] = prjDependency;
}
return getBuildOrder(n4jsPrjs);
}
use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.
the class BuildOrderComputer method getDependencies.
private Set<N4JSProject> getDependencies(IN4JSProject project) {
Set<N4JSProject> prjDependencies = new HashSet<>();
ImmutableList<? extends IN4JSSourceContainerAware> deps = project.getAllDirectDependencies();
for (IN4JSSourceContainerAware dep : deps) {
IN4JSProject pDep = core.findProject(dep.getLocation()).orNull();
boolean isValidDep = true;
isValidDep &= pDep != null && pDep.exists();
isValidDep &= !(pDep instanceof N4JSEclipseProject) || ((N4JSEclipseProject) pDep).getProject().isOpen();
if (isValidDep) {
prjDependencies.add((N4JSProject) pDep);
}
}
return prjDependencies;
}
use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.
the class BuildOrderComputer method computeVertexOrder.
private VertexOrder<String> computeVertexOrder(String[] requestedProjectURIs) {
// init worklist with existing projects
TreeSet<String> worklist = new TreeSet<>();
for (String projectURI : requestedProjectURIs) {
IN4JSProject project = uri2PrjMapper.get(projectURI);
if (project != null && project.exists()) {
worklist.add(projectURI);
}
}
// find dependency edges and project closure
Multimap<String, String> edges = HashMultimap.create();
TreeSet<String> projectsClosure = new TreeSet<>();
while (!worklist.isEmpty()) {
String projectURI = worklist.pollFirst();
projectsClosure.add(projectURI);
IN4JSProject project = uri2PrjMapper.get(projectURI);
Set<N4JSProject> prjDependencies = getDependencies(project);
Set<String> prjDependencyURIs = new HashSet<>();
// add edges
for (N4JSProject prjDependency : prjDependencies) {
String depURI = prjDependency.getLocation().toString();
edges.put(projectURI, depURI);
prjDependencyURIs.add(depURI);
}
// add only new dependencies to the worklist
prjDependencyURIs.removeAll(projectsClosure);
worklist.addAll(prjDependencyURIs);
}
// transform dependency edges
List<String[]> edges2 = new ArrayList<>();
for (Map.Entry<String, String> dependency : edges.entries()) {
String prj = dependency.getKey();
String depOn = dependency.getValue();
edges2.add(new String[] { prj, depOn });
}
VertexOrder<Object> orderedObjects = ComputeProjectOrder.computeVertexOrder(projectsClosure, edges2);
VertexOrder<String> orderedURIs = ComputeProjectOrder.castVertexOrder(orderedObjects, String.class);
return orderedURIs;
}
use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.
the class N4JSProjectsStateHelper method initVisibleHandles.
public List<String> initVisibleHandles(String handle) {
if (handle.startsWith(PROJECT_CONTAINER_PREFIX)) {
// similar to source-container-prefix but we are only interested in the project/archive and
// don't have an actual file of the source-locations.
URI uri = URI.createURI(handle.substring(PROJECT_CONTAINER_PREFIX.length()));
List<String> result = Lists.newArrayList();
// Project.
Optional<? extends IN4JSEclipseProject> containerProjectOpt = core.findProject(uri);
if (containerProjectOpt.isPresent()) {
fullCollectLocationHandles(result, containerProjectOpt.get());
} else {
// archive
Optional<? extends IN4JSArchive> containerArchiveOpt = core.findArchive(uri);
if (containerArchiveOpt.isPresent()) {
// out of archive
IN4JSArchive archive = containerArchiveOpt.get();
fullCollectLocationHandles(result, archive);
} else {
// Nothing found.
return Collections.emptyList();
}
}
return result;
}
URI uri = URI.createURI(handle.substring(SOURCE_CONTAINER_PREFIX.length()));
Optional<? extends IN4JSSourceContainer> containerOpt = core.findN4JSSourceContainer(uri);
List<String> result = Lists.newArrayList();
if (containerOpt.isPresent()) {
IN4JSSourceContainer container = containerOpt.get();
if (container.isLibrary()) {
IN4JSArchive archive = container.getLibrary();
fullCollectLocationHandles(result, archive);
} else {
IN4JSProject project = container.getProject();
fullCollectLocationHandles(result, project);
}
return result;
}
return Collections.emptyList();
}
Aggregations