use of org.spoofax.terms.StrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method completionErroneousProgramsNested.
public Collection<? extends ICompletion> completionErroneousProgramsNested(int cursorPosition, Collection<IStrategoTerm> nestedCompletionTerms, ISpoofaxParseUnit completionParseResult) throws MetaborgException {
final FileObject location = completionParseResult.source();
final ILanguageImpl language = completionParseResult.input().langImpl();
final String languageName = language.belongsTo().name();
final Collection<ICompletion> completions = Lists.newLinkedList();
IStrategoTerm completionAst = completionParseResult.ast();
for (ILanguageComponent component : language.components()) {
final ITermFactory termFactory = termFactoryService.get(component, null, false);
for (IStrategoTerm nestedCompletionTerm : nestedCompletionTerms) {
final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
Collection<IStrategoTerm> inputsStrategoNested = Lists.newLinkedList();
// calculate direct proposals
inputsStrategoNested.addAll(calculateDirectCompletionProposals(nestedCompletionTerm, termFactory, completionAst, languageName, runtime));
// calculate inner nested proposals
Collection<IStrategoTerm> innerNestedCompletionTerms = findNestedCompletionTerm((StrategoTerm) nestedCompletionTerm, true);
for (IStrategoTerm innerNestedCompletionTerm : innerNestedCompletionTerms) {
inputsStrategoNested.addAll(calculateNestedCompletionProposals(nestedCompletionTerm, innerNestedCompletionTerm, termFactory, completionAst, languageName, runtime));
}
for (IStrategoTerm inputStrategoNested : inputsStrategoNested) {
final IStrategoTerm proposalTermNested = strategoCommon.invoke(runtime, inputStrategoNested, "get-proposals-incorrect-programs-nested-" + languageName);
if (proposalTermNested == null) {
logger.error("Getting proposals for {} failed", inputStrategoNested);
continue;
}
final String name = Tools.asJavaString(proposalTermNested.getSubterm(0));
final String text = Tools.asJavaString(proposalTermNested.getSubterm(1));
final String additionalInfo = Tools.asJavaString(proposalTermNested.getSubterm(2));
final StrategoAppl change = (StrategoAppl) proposalTermNested.getSubterm(3);
final StrategoTerm completionTerm = (StrategoTerm) proposalTermNested.getSubterm(4);
String prefix = calculatePrefix(cursorPosition, completionTerm);
String suffix = calculateSuffix(cursorPosition, completionTerm);
String completionKind = "recovery";
// if the change is inserting at the end of a list
if (change.getConstructor().getName().contains("INSERT_AT_END")) {
// calls a different method because now, the program has errors that should be fixed
final ICompletion completion = createCompletionInsertAtEndFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
continue;
}
completions.add(completion);
} else if (change.getConstructor().getName().contains("INSERT_BEFORE")) {
final ICompletion completion = createCompletionInsertBeforeFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
continue;
}
completions.add(completion);
} else if (change.getConstructor().getName().contains("INSERTION_TERM")) {
final ICompletion completion = createCompletionInsertionTermFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
continue;
}
completions.add(completion);
} else if (change.getConstructor().getName().contains("REPLACE_TERM")) {
final ICompletion completion = createCompletionReplaceTermFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
continue;
}
completions.add(completion);
}
}
}
}
return completions;
}
use of org.spoofax.terms.StrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method calculateDirectCompletionProposals.
private Collection<IStrategoTerm> calculateDirectCompletionProposals(IStrategoTerm nestedCompletionTerm, ITermFactory termFactory, IStrategoTerm completionAst, String languageName, HybridInterpreter runtime) throws MetaborgException {
Collection<IStrategoTerm> inputsStratego = Lists.newLinkedList();
Collection<IStrategoTerm> completionTerms = findCompletionTermInsideNested((StrategoTerm) nestedCompletionTerm);
for (IStrategoTerm completionTerm : completionTerms) {
final StrategoTerm topMostCompletionTerm = findTopMostCompletionNode((StrategoTerm) completionTerm);
final StrategoTerm topMostAmb = findTopMostAmbNode(topMostCompletionTerm);
IStrategoTerm parenthesized = parenthesizeTerm(topMostCompletionTerm, termFactory);
final IStrategoTerm inputStratego = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, completionTerm, topMostAmb, parenthesized);
final IStrategoTerm proposalTerm = strategoCommon.invoke(runtime, inputStratego, "get-proposals-incorrect-programs-" + languageName);
if (proposalTerm == null) {
logger.error("Getting proposals for {} failed", inputStratego);
continue;
}
final IStrategoTerm replaceTermText = termFactory.makeAppl(new StrategoConstructor("REPLACE_TERM_TEXT", 2), topMostAmb, proposalTerm.getSubterm(1));
IStrategoTerm parenthesizedNested = parenthesizeTerm(nestedCompletionTerm, termFactory);
final IStrategoTerm inputStrategoNested = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesizedNested)), completionAst, nestedCompletionTerm, proposalTerm.getSubterm(0), replaceTermText, parenthesizedNested);
inputsStratego.add(inputStrategoNested);
}
return inputsStratego;
}
use of org.spoofax.terms.StrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method createCompletionReplaceTermFixing.
private ICompletion createCompletionReplaceTermFixing(String name, String text, String additionalInfo, String prefix, String suffix, StrategoAppl change, String completionKind) {
final StrategoTerm oldNode = (StrategoTerm) change.getSubterm(0);
final StrategoTerm newNode = (StrategoTerm) change.getSubterm(1);
if (change.getSubtermCount() != 2 || !(newNode instanceof IStrategoAppl) || !(oldNode instanceof IStrategoAppl)) {
return null;
}
final String sort = ImploderAttachment.getSort(oldNode);
int insertionPoint, suffixPoint;
final ImploderAttachment oldNodeIA = oldNode.getAttachment(ImploderAttachment.TYPE);
ITokens tokenizer = ImploderAttachment.getTokenizer(oldNode);
// check if it's an empty node
if (oldNodeIA.getLeftToken().getStartOffset() > oldNodeIA.getRightToken().getEndOffset()) {
// get the last non-layout token before the new node
int tokenPosition = oldNodeIA.getLeftToken().getIndex() - 1 > 0 ? oldNodeIA.getLeftToken().getIndex() - 1 : 0;
while ((tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_ERROR) && tokenPosition > 0) tokenPosition--;
insertionPoint = tokenizer.getTokenAt(tokenPosition).getEndOffset();
} else {
// if not, do a regular replacement
insertionPoint = oldNodeIA.getLeftToken().getStartOffset() - 1;
}
// insert after the first non-layout token
int tokenPositionEnd = oldNodeIA.getRightToken().getIndex();
while ((tokenizer.getTokenAt(tokenPositionEnd).getEndOffset() < tokenizer.getTokenAt(tokenPositionEnd).getStartOffset() || tokenizer.getTokenAt(tokenPositionEnd).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(tokenPositionEnd).getKind() == IToken.TK_ERROR) && tokenPositionEnd > 0) tokenPositionEnd--;
suffixPoint = tokenizer.getTokenAt(tokenPositionEnd).getEndOffset() + 1;
CompletionKind kind;
if (completionKind.equals("recovery")) {
kind = CompletionKind.recovery;
} else {
kind = CompletionKind.expansion;
}
return new Completion(name, sort, text, additionalInfo, insertionPoint + 1, suffixPoint, kind, prefix, suffix);
}
use of org.spoofax.terms.StrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method findTopMostCompletionNode.
private StrategoTerm findTopMostCompletionNode(StrategoTerm newNode) {
StrategoTerm parent = (StrategoTerm) ParentAttachment.getParent(newNode);
if (parent == null) {
return newNode;
}
ImploderAttachment ia = ImploderAttachment.get(parent);
if (ia.getSort() == null || ia.isNestedCompletion()) {
return newNode;
}
return findTopMostCompletionNode(parent);
}
use of org.spoofax.terms.StrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method calculateNestedCompletionProposals.
private Collection<IStrategoTerm> calculateNestedCompletionProposals(IStrategoTerm mainNestedCompletionTerm, IStrategoTerm nestedCompletionTerm, ITermFactory termFactory, IStrategoTerm completionAst, String languageName, HybridInterpreter runtime) throws MetaborgException {
Collection<IStrategoTerm> inputsStratego = Lists.newLinkedList();
Collection<IStrategoTerm> nestedCompletionTerms = findNestedCompletionTerm((StrategoTerm) nestedCompletionTerm, true);
for (IStrategoTerm innerNestedCompletionTerm : nestedCompletionTerms) {
Collection<IStrategoTerm> inputsStrategoInnerNested = calculateNestedCompletionProposals(nestedCompletionTerm, innerNestedCompletionTerm, termFactory, completionAst, languageName, runtime);
for (IStrategoTerm inputStrategoNested : inputsStrategoInnerNested) {
final IStrategoTerm proposalTermNested = strategoCommon.invoke(runtime, inputStrategoNested, "get-proposals-incorrect-programs-nested-" + languageName);
if (proposalTermNested == null) {
logger.error("Getting proposals for {} failed", inputStrategoNested);
continue;
}
final StrategoTerm topMostAmb = findTopMostAmbNode((StrategoTerm) nestedCompletionTerm);
final IStrategoTerm replaceTermText = termFactory.makeAppl(new StrategoConstructor("REPLACE_TERM_TEXT", 2), topMostAmb, proposalTermNested.getSubterm(1));
IStrategoTerm parenthesized = parenthesizeTerm(mainNestedCompletionTerm, termFactory);
final IStrategoTerm inputStrategoInnerNested = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, mainNestedCompletionTerm, proposalTermNested.getSubterm(0), replaceTermText, parenthesized);
inputsStratego.add(inputStrategoInnerNested);
}
}
Collection<IStrategoTerm> inputsStrategoInner = calculateDirectCompletionProposals(nestedCompletionTerm, termFactory, completionAst, languageName, runtime);
for (IStrategoTerm inputStrategoNested : inputsStrategoInner) {
final IStrategoTerm proposalTermNested = strategoCommon.invoke(runtime, inputStrategoNested, "get-proposals-incorrect-programs-nested-" + languageName);
if (proposalTermNested == null) {
logger.error("Getting proposals for {} failed", inputStrategoNested);
continue;
}
final StrategoTerm topMostAmb = findTopMostAmbNode((StrategoTerm) nestedCompletionTerm);
final IStrategoTerm replaceTermText = termFactory.makeAppl(new StrategoConstructor("REPLACE_TERM_TEXT", 2), topMostAmb, proposalTermNested.getSubterm(1));
IStrategoTerm parenthesized = parenthesizeTerm(mainNestedCompletionTerm, termFactory);
final IStrategoTerm inputStrategoInnerNested = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, mainNestedCompletionTerm, proposalTermNested.getSubterm(0), replaceTermText, parenthesized);
inputsStratego.add(inputStrategoInnerNested);
}
return inputsStratego;
}
Aggregations