use of org.eclipse.n4js.jsdoc.dom.FullMemberReference in project n4js by eclipse.
the class LineTagWithFullElementReference method parseReference.
private FullMemberReference parseReference(JSDocCharScanner scanner, int maxOffset) {
scanner.skipWS();
int start = scanner.offset();
StringBuilder[] segments = new StringBuilder[3];
for (int i = 0; i < segments.length; i++) {
segments[i] = new StringBuilder();
}
int segment = 0;
boolean foundStatic = false;
char prev = 0;
while (scanner.offset() <= maxOffset && scanner.hasNext() && !Character.isWhitespace(scanner.peek())) {
char c = scanner.next();
if (//
(c == '.' || c == '#') && !(segment == 2 && (prev == '.' || prev == '#'))) {
// be aware of symbols starting with #
segment++;
if (segment > 2) {
break;
}
if (c == '#') {
if (segment == 2) {
foundStatic = true;
} else {
// error
break;
}
}
} else {
segments[segment].append(c);
}
prev = c;
}
FullMemberReference ref = DomFactory.eINSTANCE.createFullMemberReference();
ref.setRange(start, scanner.offset());
ref.setModuleName(segments[0].toString());
ref.setTypeName(segments[1].toString());
ref.setMemberName(segments[2].toString());
ref.setStaticMember(foundStatic);
return ref;
}
use of org.eclipse.n4js.jsdoc.dom.FullMemberReference in project n4js by eclipse.
the class LineTagWithFullElementReference method completionHint.
@Override
public JSDocCompletionHint completionHint(JSDocCharScanner scanner) {
int start = scanner.offset();
scanner.rewindToWS();
scanner.next();
FullMemberReference ref = parseReference(scanner, start);
String prefix = ref.toString();
char lastChar = scanner.charAt(start);
if (!(Character.isWhitespace(lastChar) || prefix.endsWith(Character.toString(lastChar)))) {
prefix += lastChar;
}
return new JSDocCompletionHint(FQMEMBER, prefix, ref);
}
use of org.eclipse.n4js.jsdoc.dom.FullMemberReference in project n4js by eclipse.
the class LineTagWithFullMemberReferenceTest method test_fullRef_simpleName.
@SuppressWarnings("javadoc")
@Test
public void test_fullRef_simpleName() {
String in = "/** foo." + "\n * @testee n4/model/collections/DataSet.DataSet.each" + "\n */";
AbstractLineTagDefinition tagDef = new LineTagWithFullMemberReference("testee");
DocletParser docletParser = new DocletParser(new TagDictionary<>(Arrays.asList(tagDef)), new TagDictionary<AbstractInlineTagDefinition>());
Doclet doclet = docletParser.parse(in);
LineTag lineTag = doclet.getLineTags().get(0);
EList<ContentNode> contents = lineTag.getValueByKey(LineTagWithFullElementReference.REF).getContents();
FullMemberReference ref = (FullMemberReference) contents.get(0);
assertEquals("n4/model/collections/DataSet", ref.getModuleName());
assertEquals("DataSet", ref.getTypeName());
assertEquals("each", ref.getMemberName());
}
use of org.eclipse.n4js.jsdoc.dom.FullMemberReference in project n4js by eclipse.
the class JSDocCompletionProposalComputer method exec.
@SuppressWarnings("unchecked")
@Override
public ICompletionProposal[] exec(XtextResource xtextResource) throws Exception {
ContentAssistContext[] contexts = processor.getContextFactory().create(viewer, offset, xtextResource);
if (contexts.length > 0) {
ArrayList<ICompletionProposal> proposals = new ArrayList<>();
ContentAssistContext contentAssistContext = contexts[contexts.length - 1];
INode currentNode = contentAssistContext.getCurrentNode();
String content = currentNode.getText();
int offsetInNode = contentAssistContext.getOffset() - currentNode.getOffset() - contentAssistContext.getPrefix().length();
// String textInFront = content.substring(0, offsetInNode);
// System.out.println(textInFront);
N4JSDocletParser docletParser = processor.getDocletParser();
Doclet doclet = docletParser.parse(content);
Optional<String> lineTagPrefix = getLineTagTitlePrefix(content, offsetInNode);
if (lineTagPrefix.isPresent()) {
createLineTagProposal(lineTagPrefix.get(), docletParser, proposals);
}
Tag tag = JSDocletUtils.getTagAtOffset(doclet, offsetInNode);
if (tag != null) {
ITagDefinition tagDef = tag.getTagDefinition();
JSDocCharScanner scanner = new JSDocCharScanner(content);
ScannerState state = scanner.saveState();
scanner.setNextOffset(offsetInNode);
JSDocCompletionHint completionHint = tagDef.completionHint(scanner);
scanner.restoreState(state);
if (completionHint.kind != NOCOMPLETION) {
int replacementOffset = offset - completionHint.prefix.length();
if (completionHint.isTypeModelRef()) {
// get reference as far as it can be parsed:
FullMemberReference ref = completionHint.nodeAsFullMemberReference();
IContentAssistScopeProvider scopeProvider = (IContentAssistScopeProvider) processor.getScopeProvider();
IScope moduleSpecScope = scopeProvider.getScopeForContentAssist(// context for finding modules
xtextResource.getContents().get(0), N4JSPackage.Literals.IMPORT_DECLARATION__MODULE);
// complete module or type
if (!completionHint.isModuleNameCompleted()) {
for (IEObjectDescription moduleDescr : moduleSpecScope.getAllElements()) {
String moduleSpec = moduleDescr.getName().toString("/");
String moduleSimpleName = moduleDescr.getName().getLastSegment();
if (!moduleSpec.startsWith("#")) {
if (moduleSpec.startsWith(completionHint.prefix) || moduleSimpleName.startsWith(completionHint.prefix)) {
if (moduleSpec.length() == completionHint.prefix.length()) {
int replacementLength = 0;
int cursorPosition = 1;
ICompletionProposal proposal = new CompletionProposal(".", replacementOffset + completionHint.prefix.length(), replacementLength, cursorPosition);
proposals.add(proposal);
} else {
int replacementLength = completionHint.prefix.length();
int cursorPosition = moduleSpec.length();
ICompletionProposal proposal = new CompletionProposal(moduleSpec, replacementOffset, replacementLength, cursorPosition);
proposals.add(proposal);
}
}
}
}
} else {
QualifiedName moduleQN = QualifiedName.create(ref.getModuleName().split("/"));
IEObjectDescription descr = moduleSpecScope.getSingleElement(moduleQN);
if (descr != null && descr.getEObjectOrProxy() instanceof TModule) {
TModule module = (TModule) descr.getEObjectOrProxy();
if (module.eIsProxy())
module = (TModule) EcoreUtil.resolve(module, xtextResource);
if (!completionHint.isTypeNameCompleted(false) && completionHint.kind != MODULESPEC) {
String typePrefix = ref.getTypeName();
for (Type t : module.getTopLevelTypes()) {
String typeName = t.getName();
if (typeName.startsWith(typePrefix)) {
String completion = module.getModuleSpecifier() + "." + typeName;
int replacementLength = completionHint.prefix.length();
int cursorPosition = completion.length();
ICompletionProposal proposal = new CompletionProposal(completion, replacementOffset, replacementLength, cursorPosition);
proposals.add(proposal);
}
}
} else {
// completionHint.kind == MEMBER
Optional<Type> optType = module.getTopLevelTypes().stream().filter(t -> t.getName().equals(ref.getTypeName())).findAny();
if (optType.isPresent()) {
Type t = optType.get();
if (t instanceof ContainerType) {
String memberPrefix = ref.getMemberName();
for (TMember m : ((ContainerType<? extends TMember>) t).getOwnedMembers()) {
String memberName = m.getName();
if (memberName.startsWith(memberPrefix)) {
String completion = LineTagWithFullElementReference.createReferenceFromType(m).toString();
int replacementLength = completionHint.prefix.length();
int cursorPosition = completion.length();
ICompletionProposal proposal = new CompletionProposal(completion, replacementOffset, replacementLength, cursorPosition);
proposals.add(proposal);
}
}
}
}
}
}
}
}
}
}
ICompletionProposal[] result = new ICompletionProposal[proposals.size()];
proposals.toArray(result);
return result;
}
return null;
}
use of org.eclipse.n4js.jsdoc.dom.FullMemberReference in project n4js by eclipse.
the class LineTagWithFullMemberReferenceTest method test_fullRef_symbolName.
@SuppressWarnings("javadoc")
@Test
public void test_fullRef_symbolName() {
String in = "/** foo." + "\n * @testee n4/model/collections/DataSet.DataSet.#iterator" + "\n */";
AbstractLineTagDefinition tagDef = new LineTagWithFullMemberReference("testee");
DocletParser docletParser = new DocletParser(new TagDictionary<>(Arrays.asList(tagDef)), new TagDictionary<AbstractInlineTagDefinition>());
Doclet doclet = docletParser.parse(in);
LineTag lineTag = doclet.getLineTags().get(0);
EList<ContentNode> contents = lineTag.getValueByKey(LineTagWithFullElementReference.REF).getContents();
FullMemberReference ref = (FullMemberReference) contents.get(0);
assertEquals("n4/model/collections/DataSet", ref.getModuleName());
assertEquals("DataSet", ref.getTypeName());
assertEquals("#iterator", ref.getMemberName());
}
Aggregations