org.eclipse.jface.text.hyperlink.IHyperlink Java Examples

The following examples show how to use org.eclipse.jface.text.hyperlink.IHyperlink. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSHyperlinkDetector.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * detectHyperlinks
 * 
 * @param editor
 * @param region
 * @param canShowMultipleHyperlinks
 * @return
 */
public IHyperlink[] detectHyperlinks(AbstractThemeableEditor editor, IRegion region,
		boolean canShowMultipleHyperlinks)
{
	IHyperlink[] result = null;

	if (editor != null && region != null)
	{
		// grab AST
		IParseNode ast = editor.getAST();

		if (ast instanceof JSParseRootNode)
		{
			// gather links
			result = processAST(editor, (JSParseRootNode) ast, region.getOffset());
		}
	}

	if (!canShowMultipleHyperlinks && !ArrayUtil.isEmpty(result))
	{
		result = new IHyperlink[] { result[0] };
	}

	return result;
}
 
Example #2
Source File: N4JSHyperlinkDetector.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Method copied from super class with only a minor change: call to "readOnly" changed to "tryReadOnly".
 */
@Override
public IHyperlink[] detectHyperlinks(final ITextViewer textViewer, final IRegion region,
		final boolean canShowMultipleHyperlinks) {
	final IDocument xtextDocument = textViewer.getDocument();
	if (!(xtextDocument instanceof N4JSDocument)) {
		return super.detectHyperlinks(textViewer, region, canShowMultipleHyperlinks);
	}
	final IHyperlinkHelper helper = getHelper();
	return ((N4JSDocument) xtextDocument).tryReadOnly(new IUnitOfWork<IHyperlink[], XtextResource>() {
		@Override
		public IHyperlink[] exec(XtextResource resource) throws Exception {
			resource = tryConvertToFileResource(resource);
			if (resource == null) {
				return null;
			}
			if (helper instanceof ISourceViewerAware && textViewer instanceof ISourceViewer) {
				((ISourceViewerAware) helper).setSourceViewer((ISourceViewer) textViewer);
			}
			return helper.createHyperlinksByOffset(resource, region.getOffset(), canShowMultipleHyperlinks);
		}
	}, (IHyperlink[]) null);
}
 
Example #3
Source File: PackageJsonHyperlinkHelperExtension.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public IHyperlink[] getHyperlinks(XtextResource resource, int offset) {
	EObject eObject = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
	Pair<SafeURI<?>, Region> linkedProjectWithRegion = getUriRegionPair(eObject);

	if (linkedProjectWithRegion != null) {
		URI uri = linkedProjectWithRegion.getFirst().toURI();
		IRegion region = linkedProjectWithRegion.getSecond();
		IN4JSProject uriProject = model.findProject(uri).orNull();

		String lnkName = uriProject == null ? ProjectDescriptionUtils.deriveN4JSProjectNameFromURI(uri)
				: uriProject.getProjectName().getRawName();

		XtextHyperlink hyperlink = hyperlinkProvider.get();
		hyperlink.setHyperlinkRegion(region);
		hyperlink.setURI(uri);
		hyperlink.setHyperlinkText(lnkName);

		return new IHyperlink[] { hyperlink };
	}

	return null;
}
 
Example #4
Source File: JSONHyperlinkHelperProvider.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public IHyperlink[] createHyperlinksByOffset(XtextResource resource, int offset, boolean createMultipleHyperlinks) {
	List<IHyperlink> links = new LinkedList<>();

	Collection<IJSONHyperlinkHelperExtension> hyperlinkHelperExtensions = registry.getHyperlinkHelperExtensions();
	for (IJSONHyperlinkHelperExtension hhExt : hyperlinkHelperExtensions) {
		IHyperlink[] hyperlinks = null;
		if (hhExt.isResponsible(resource)) {
			hyperlinks = hhExt.getHyperlinks(resource, offset);
		}
		if (hyperlinks == null) {
			continue;
		}

		List<IHyperlink> hyperlinkList = Arrays.asList(hyperlinks);
		links.addAll(hyperlinkList);
	}

	if (links.isEmpty()) {
		return null;
	}

	return links.toArray(new IHyperlink[links.size()]);
}
 
Example #5
Source File: OpenImplHyperlinksTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testComputeHyperlink_2() throws Exception {
	String content = "package foo interface IBar {def void bar(String a)} class Foo implements IBar { override bar(String a){ val IBar x = null; x.b|ar('foo'} } class Foo2 implements IBar { override bar(String a) {} }";
	XtextEditor xtextEditor = openEditor(content.replace("|", ""));
	int offset = content.indexOf("|");
	IHyperlink[] detectHyperlinks = hyperlinkDetector.detectHyperlinks(xtextEditor.getInternalSourceViewer(), new Region(offset,1), true);
	XbaseImplementatorsHyperlink casted = null;
	for (IHyperlink iHyperlink : detectHyperlinks) {
		if (iHyperlink instanceof XbaseImplementatorsHyperlink) {
			casted = (XbaseImplementatorsHyperlink) iHyperlink;
		}
	}
	@SuppressWarnings("null")
	Field field = casted.getClass().getDeclaredField("opener");
	field.setAccessible(true);
	TestJvmImplementationOpener testOpener = new TestJvmImplementationOpener();
	field.set(casted, testOpener);
	casted.open();
	assertFalse(testOpener.isOpenInEditor);
	assertTrue(testOpener.isOpenQuickHierarchy);

}
 
Example #6
Source File: HyperlinkHelperTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testCreateHyperlinksByOffset_06() {
	assertNull(helper.createHyperlinksByOffset(resource, model.indexOf(":EObject]"), true));
	int idx = model.indexOf("EObject]");
	int length = "EObject".length();
	for(int i = idx; i <= length; i++) {
		IHyperlink[] hyperlinks = helper.createHyperlinksByOffset(resource, i, true);
		assertNotNull(hyperlinks);
		assertEquals(1, hyperlinks.length);
		assertTrue(hyperlinks[0] instanceof XtextHyperlink);
		XtextHyperlink hyperLink = (XtextHyperlink) hyperlinks[0];
		assertEquals("EObject".length(), hyperLink.getHyperlinkRegion().getLength());
		assertEquals(
				grammar.eResource().getResourceSet().getURIConverter().normalize(
				EcoreUtil.getURI(EcorePackage.eINSTANCE.getEObject())), hyperLink.getURI());
	}
}
 
Example #7
Source File: SingleHoverShowingHyperlinkPresenter.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected IHyperlink[] makeNullsafe(IHyperlink[] hyperlinks) {
	if (Arrays.stream(hyperlinks).anyMatch(hyperLink -> (hyperLink == null || hyperLink.getHyperlinkRegion() == null))) {
		List<IHyperlink> nullSafeList = new ArrayList<>();
		Arrays.stream(hyperlinks).forEach(hyperLink -> {
			if (hyperLink != null && hyperLink.getHyperlinkRegion() != null) {
				nullSafeList.add(hyperLink);
			} else {
				Class<? extends IHyperlink> clazz = null;
				if (hyperLink != null) {
					clazz = hyperLink.getClass();
				}
				String name = null;
				if (clazz != null) {
					name = clazz.getName();
				}
				log.warn("Filtered invalid hyperlink: " + name);
			}
		});
		return nullSafeList.toArray(new IHyperlink[nullSafeList.size()]);
	}
	return hyperlinks;
}
 
Example #8
Source File: OpenDeclarationHandler.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
	XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
	if (xtextEditor != null) {
		ITextSelection selection = (ITextSelection) xtextEditor.getSelectionProvider().getSelection();

		IRegion region = new Region(selection.getOffset(), selection.getLength());

		ISourceViewer internalSourceViewer = xtextEditor.getInternalSourceViewer();

		IHyperlink[] hyperlinks = getDetector().detectHyperlinks(internalSourceViewer, region, false);
		if (hyperlinks != null && hyperlinks.length > 0) {
			IHyperlink hyperlink = hyperlinks[0];
			hyperlink.open();
		}
	}		
	return null;
}
 
Example #9
Source File: XbaseHyperLinkHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * If multiple links are requested, all ambiguous candidates are provided for feature and constructor calls.
 */
@Override
public IHyperlink[] createHyperlinksByOffset(XtextResource resource, int offset, boolean createMultipleHyperlinks) {
	List<IHyperlink> links = Lists.newArrayList();
	IHyperlinkAcceptor acceptor = new XbaseHyperlinkAcceptor(links, createMultipleHyperlinks);
	super.createHyperlinksByOffset(resource, offset, acceptor);
	INode crossRefNode = getEObjectAtOffsetHelper().getCrossReferenceNode(resource, new TextRegion(offset, 0));
	if (crossRefNode == null) {
		this.createHyperlinksByOffset(resource, offset, acceptor);
	} else {
		this.createHyperlinksForCrossRef(resource, crossRefNode, acceptor);
	}
	if (!links.isEmpty())
		return Iterables.toArray(links, IHyperlink.class);
	return null;
}
 
Example #10
Source File: XbaseHyperLinkHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void addOpenInferredTypeHyperlink(final XtextResource resource, JvmIdentifiableElement typedElement,
		ILeafNode node, final IHyperlinkAcceptor acceptor) {
	IResolvedTypes resolveTypes = typeResolver.resolveTypes(resource);
	final LightweightTypeReference type = resolveTypes.getActualType(typedElement);
	if (type != null && !type.isUnknown() && type.getType() != null) {
		createHyperlinksTo(resource, new Region(node.getOffset(), node.getLength()), type.getType(), new IHyperlinkAcceptor() {
			@Override
			public void accept(IHyperlink hyperlink) {
				if (hyperlink instanceof AbstractHyperlink) {
					AbstractHyperlink abstractHyperlink = (AbstractHyperlink) hyperlink;
					abstractHyperlink.setHyperlinkText("Open Inferred Type - " + type.getSimpleName());
					abstractHyperlink.setTypeLabel(SingleHoverShowingHyperlinkPresenter.SHOW_ALWAYS);
				}
				acceptor.accept(hyperlink);
			}
		});
	}
}
 
Example #11
Source File: DetectImplHyperlinksTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testComputeHyperlink_1() throws Exception {
	String content = "package foo class Foo { def b|ar(String a) {} }";
	XtextEditor xtextEditor = openEditor(content.replace("|", ""));
	int offset = content.indexOf("|");

	IHyperlink[] detectHyperlinks = hyperlinkDetector.detectHyperlinks(xtextEditor.getInternalSourceViewer(), new Region(offset,1), true);
	assertEquals(2, detectHyperlinks.length);
	IHyperlink hyperlink = detectHyperlinks[0];
	assertTrue(hyperlink instanceof JdtHyperlink);
	JdtHyperlink casted = (JdtHyperlink) hyperlink;
	assertEquals(offset -1, casted.getHyperlinkRegion().getOffset());
	assertEquals(3, casted.getHyperlinkRegion().getLength());
	IJavaElement element = ((JdtHyperlink) hyperlink).getJavaElement();
	assertTrue(element instanceof IType);
	assertEquals("Object", element.getElementName());
	assertEquals("Open Inferred Type - Object", casted.getHyperlinkText());
}
 
Example #12
Source File: OpenImplHyperlinksTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testComputeHyperlink_1() throws Exception {
	String content = "package foo class Foo implements IBar { override bar(String a) { b|ar('foo') } } interface IBar { def String bar(String a) }";
	XtextEditor xtextEditor = openEditor(content.replace("|", ""));
	int offset = content.indexOf("|");
	IHyperlink[] detectHyperlinks = hyperlinkDetector.detectHyperlinks(xtextEditor.getInternalSourceViewer(), new Region(offset,1), true);
	XbaseImplementatorsHyperlink casted = null;
	for (IHyperlink iHyperlink : detectHyperlinks) {
		if (iHyperlink instanceof XbaseImplementatorsHyperlink) {
			casted = (XbaseImplementatorsHyperlink) iHyperlink;
		}
	}
	@SuppressWarnings("null")
	Field field = casted.getClass().getDeclaredField("opener");
	field.setAccessible(true);
	TestJvmImplementationOpener testOpener = new TestJvmImplementationOpener();
	field.set(casted, testOpener);
	casted.open();
	assertTrue(testOpener.isOpenInEditor);
	assertFalse(testOpener.isOpenQuickHierarchy);
}
 
Example #13
Source File: JavaElementHyperlinkDeclaredTypeDetector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void addHyperlinks(List<IHyperlink> hyperlinksCollector, IRegion wordRegion, SelectionDispatchAction openAction, IJavaElement element, boolean qualify, JavaEditor editor) {
	try {
		if (element.getElementType() == IJavaElement.FIELD || element.getElementType() == IJavaElement.LOCAL_VARIABLE) {
			String typeSignature= getTypeSignature(element);
			if (!JavaModelUtil.isPrimitive(typeSignature) && SelectionConverter.canOperateOn(editor)) {
				if (Signature.getTypeSignatureKind(typeSignature) == Signature.INTERSECTION_TYPE_SIGNATURE) {
					String[] bounds= Signature.getIntersectionTypeBounds(typeSignature);
					qualify|= bounds.length >= 2;
					for (int i= 0; i < bounds.length; i++) {
						hyperlinksCollector.add(new JavaElementDeclaredTypeHyperlink(wordRegion, openAction, element, bounds[i], qualify));
					}
				} else {
					hyperlinksCollector.add(new JavaElementDeclaredTypeHyperlink(wordRegion, openAction, element, qualify));
				}
			}
		}
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}
}
 
Example #14
Source File: AbstractJsonHyperlinkDetector.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    JsonDocument document = (JsonDocument) textViewer.getDocument();
    JsonPointer basePath = document.getPath(region);

    if (!canDetect(basePath)) {
        return null;
    }

    HyperlinkInfo info = getHyperlinkInfo(textViewer, region);
    if (info == null) {
        return null;
    }

    return doDetect(document, textViewer, info, basePath);
}
 
Example #15
Source File: LinkOperationHyperlinkDetector.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected IHyperlink[] doDetect(JsonDocument doc, ITextViewer viewer, HyperlinkInfo info, JsonPointer pointer) {
    Model model = doc.getModel();
    AbstractNode node = model.find(pointer);
    List<AbstractNode> nodes = model.findByType(JsonPointer.compile("/definitions/operation"));
    Iterator<AbstractNode> it = nodes.iterator();

    AbstractNode found = null;
    while (it.hasNext() && found == null) {
        AbstractNode current = it.next();
        AbstractNode value = current.get("operationId");

        if (value != null && Objects.equals(node.asValue().getValue(), value.asValue().getValue())) {
            found = value;
        }
    }

    if (found != null) {
        IRegion target = doc.getRegion(found.getPointer());
        if (target != null) {
            return new IHyperlink[] { new SwaggerHyperlink(info.text, viewer, info.region, target) };
        }
    }

    return null;
}
 
Example #16
Source File: SecuritySchemeHyperlinkDetector.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected IHyperlink[] doDetect(JsonDocument doc, ITextViewer viewer, HyperlinkInfo info, JsonPointer pointer) {
    Matcher matcher = PATTERN.matcher(pointer.toString());
    String link = matcher.find() ? matcher.group(1) : null;

    if (link != null) {
        Model model = doc.getModel();
        AbstractNode securityScheme = model.find("/components/securitySchemes/" + link);

        if (securityScheme != null) {
            IRegion target = doc.getRegion(securityScheme.getPointer());
            if (target != null) {
                return new IHyperlink[] { new SwaggerHyperlink(info.text, viewer, info.region, target) };
            }
        }
    }
    return null;
}
 
Example #17
Source File: TypeScriptHyperLinkDetector.java    From typescript.java with MIT License 5 votes vote down vote up
/**
 * Create HyperLink list from the given TypeScript file spans.
 * 
 * @param spans
 * @param region
 * @return
 */
private IHyperlink[] createHyperlinks(List<FileSpan> spans, IRegion region) {
	if (spans == null || spans.size() < 1) {
		return null;
	}
	List<IHyperlink> hyperlinks = new ArrayList<IHyperlink>();
	for (FileSpan span : spans) {
		IHyperlink hyperlink = createHyperLink(span, region);
		if (hyperlink != null) {
			hyperlinks.add(hyperlink);
		}
	}
	return hyperlinks.size() > 0 ? hyperlinks.toArray(new IHyperlink[hyperlinks.size()]) : null;
}
 
Example #18
Source File: TLAEditor.java    From tlaplus with MIT License 5 votes vote down vote up
public Object execute(ExecutionEvent event) throws ExecutionException
{
    final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    final TLAEditor tlaEditor = activeEditor.getAdapter(TLAEditor.class);

    final ITextSelection selection = (ITextSelection) tlaEditor.getSelectionProvider().getSelection();
    final IRegion region = new Region(selection.getOffset(), selection.getLength());

    // get the detectors
    final ISourceViewer internalSourceViewer = tlaEditor.getSourceViewer();
    final IHyperlinkDetector[] hyperlinkDetectors = tlaEditor.getSourceViewerConfiguration()
            .getHyperlinkDetectors(internalSourceViewer);
    if (hyperlinkDetectors != null)
    {
        for (int i = 0; i < hyperlinkDetectors.length; i++)
        {
            // detect
            final IHyperlink[] hyperlinks = hyperlinkDetectors[i].detectHyperlinks(internalSourceViewer, region,
                    false);
            if (hyperlinks != null && hyperlinks.length > 0)
            {
                // open
                final IHyperlink hyperlink = hyperlinks[0];
                hyperlink.open();
                break;
            }
        }
    }
    return null;
}
 
Example #19
Source File: AbstractHyperlinkHelperTest.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Retrieves the hyperlinks found in a given source at a given offset.
 *
 * @param resource
 *          the resource in which to look for hyperlinks, must not be {@code null}
 * @param offset
 *          the position at which to look for hyperlinks
 * @return a list of hyperlinks, never {@code null}
 */
protected List<IHyperlink> getOffsetHyperlinks(final XtextResource resource, final int offset) {
  IHyperlink[] hyperlinks = UIThreadRunnable.syncExec(new Result<IHyperlink[]>() {
    @Override
    public IHyperlink[] run() {
      return getTestUtil().get(IHyperlinkHelper.class).createHyperlinksByOffset(resource, offset, true);
    }
  });
  return hyperlinks != null ? Arrays.asList(hyperlinks) : new ArrayList<IHyperlink>();
}
 
Example #20
Source File: HyperlinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testStaticFeatureCallWithAllColon() throws Exception {
	String modelAsString = "class C { val x = java::lang::String::valueOf('') }";
	XtextResource resource = (XtextResource) testHelper.xtendFile("C", modelAsString).eResource();
	IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, modelAsString.indexOf("lang"), true);
	assertEquals(1, hyperlinks.length);
	assertEquals("String", hyperlinks[0].getHyperlinkText());
	assertEquals(modelAsString.indexOf("java"), hyperlinks[0].getHyperlinkRegion().getOffset());
	assertEquals("java::lang::String".length(), hyperlinks[0].getHyperlinkRegion().getLength());
}
 
Example #21
Source File: JavaElementHyperlinkReturnTypeDetector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void addHyperlinks(List<IHyperlink> hyperlinksCollector, IRegion wordRegion, SelectionDispatchAction openAction, IJavaElement element, boolean qualify, JavaEditor editor) {
	try {
		if (element.getElementType() == IJavaElement.METHOD && !JavaModelUtil.isPrimitive(((IMethod)element).getReturnType()) && SelectionConverter.canOperateOn(editor)) {
			hyperlinksCollector.add(new JavaElementReturnTypeHyperlink(wordRegion, openAction, (IMethod)element, qualify));
		}
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}
}
 
Example #22
Source File: HyperlinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testStaticFeatureCall() throws Exception {
	String modelAsString = "class C { val x = java.lang.String.valueOf('') }";
	XtextResource resource = (XtextResource) testHelper.xtendFile("C", modelAsString).eResource();
	IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, modelAsString.indexOf("lang"), true);
	assertEquals(1, hyperlinks.length);
	assertEquals("String", hyperlinks[0].getHyperlinkText());
	assertEquals(modelAsString.indexOf("java"), hyperlinks[0].getHyperlinkRegion().getOffset());
	assertEquals("java.lang.String".length(), hyperlinks[0].getHyperlinkRegion().getLength());
}
 
Example #23
Source File: HyperlinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testNestedTypeLiteral() throws Exception {
	String modelAsString = "class C { val x = java.util.Map.Entry }";
	XtextResource resource = (XtextResource) testHelper.xtendFile("C", modelAsString).eResource();
	IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, modelAsString.indexOf("Entry"), true);
	assertEquals(1, hyperlinks.length);
	assertEquals("Entry<K, V>", hyperlinks[0].getHyperlinkText());
	assertEquals(modelAsString.indexOf("Entry"), hyperlinks[0].getHyperlinkRegion().getOffset());
	assertEquals("Entry".length(), hyperlinks[0].getHyperlinkRegion().getLength());
}
 
Example #24
Source File: HyperlinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testNestedTypePackageFragment() throws Exception {
	String modelAsString = "class C { val x = java.util.Map.Entry }";
	XtextResource resource = (XtextResource) testHelper.xtendFile("C", modelAsString).eResource();
	IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, modelAsString.indexOf("util"), true);
	assertEquals(1, hyperlinks.length);
	assertEquals("Map<K, V>", hyperlinks[0].getHyperlinkText());
	assertEquals(modelAsString.indexOf("java"), hyperlinks[0].getHyperlinkRegion().getOffset());
	assertEquals("java.util.Map".length(), hyperlinks[0].getHyperlinkRegion().getLength());
}
 
Example #25
Source File: HyperlinkHelperTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCreateHyperlinksByOffset_04() {
	IHyperlink[] links = helper.createHyperlinksByOffset(resource, model.indexOf("TRING r"), true);
	assertNotNull(links);
	assertEquals(1, links.length);
	assertTrue(links[0] instanceof XtextHyperlink);
	XtextHyperlink hyperLink = (XtextHyperlink) links[0];
	assertEquals(model.indexOf("STRING r"), hyperLink.getHyperlinkRegion().getOffset());
	assertEquals("STRING".length(), hyperLink.getHyperlinkRegion().getLength());
	assertEquals(
			grammar.eResource().getResourceSet().getURIConverter().normalize(
			EcoreUtil.getURI(GrammarUtil.findRuleForName(grammar.getUsedGrammars().get(0), "STRING"))), hyperLink.getURI());
}
 
Example #26
Source File: HyperlinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testPackageFragment() throws Exception {
	String modelAsString = "class C { val x = java.lang.String }";
	XtextResource resource = (XtextResource) testHelper.xtendFile("C", modelAsString).eResource();
	IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, modelAsString.indexOf("lang"), true);
	assertEquals(1, hyperlinks.length);
	assertEquals("String", hyperlinks[0].getHyperlinkText());
	assertEquals(modelAsString.indexOf("java"), hyperlinks[0].getHyperlinkRegion().getOffset());
	assertEquals("java.lang.String".length(), hyperlinks[0].getHyperlinkRegion().getLength());
}
 
Example #27
Source File: HyperlinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSuperClass() throws Exception {
	String modelAsString =
		"class Foo extends Object {\n"
			+ "}";
	XtextResource resource = (XtextResource) testHelper.xtendFile("Foo", modelAsString).eResource();
	IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, modelAsString.indexOf("Object"), true);
	assertEquals(1, hyperlinks.length);
	assertEquals("Object", hyperlinks[0].getHyperlinkText());
}
 
Example #28
Source File: HyperlinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testPlainMethod() throws Exception {
	String modelAsString = "class Foo {\n"
			+ "  def bar() { foo() }\n"
			+ "  def foo() { null }\n"
			+ "}";
	XtextResource resource = (XtextResource) testHelper.xtendFile("Foo", modelAsString).eResource();
	IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, modelAsString.indexOf("foo"), true);
	assertEquals(2, hyperlinks.length);
	assertEquals("Open Declaration - foo() : Object", hyperlinks[0].getHyperlinkText());
	assertTrue(((XtextHyperlink) hyperlinks[0]).getURI().isPlatformResource());
	assertEquals("Open Return Type - Object", hyperlinks[1].getHyperlinkText());
	assertEquals("Object", ((JdtHyperlink) hyperlinks[1]).getJavaElement().getElementName());
}
 
Example #29
Source File: HyperlinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testTypeLiteral() throws Exception {
	String modelAsString = "class C { val x = java.lang.String }";
	XtextResource resource = (XtextResource) testHelper.xtendFile("C", modelAsString).eResource();
	IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, modelAsString.indexOf("String"), true);
	assertEquals(1, hyperlinks.length);
	assertEquals("String", hyperlinks[0].getHyperlinkText());
	assertEquals(modelAsString.indexOf("java"), hyperlinks[0].getHyperlinkRegion().getOffset());
	assertEquals("java.lang.String".length(), hyperlinks[0].getHyperlinkRegion().getLength());
}
 
Example #30
Source File: JSHyperlinkDetector.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * processAST
 * 
 * @param editor
 * @param ast
 * @param offset
 * @return
 */
private IHyperlink[] processAST(AbstractThemeableEditor editor, JSParseRootNode ast, int offset)
{
	// walk AST to grab potential hyperlinks
	JSHyperlinkCollector collector = new JSHyperlinkCollector(editor, ast, offset);
	ast.accept(collector);
	Collection<? extends IHyperlink> result = collector.getHyperlinks();

	return (CollectionsUtil.isEmpty(result)) ? null : result.toArray(new IHyperlink[result.size()]);
}