use of org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition in project abstools by abstools.
the class WizardUtil method getInsertionPosition.
/**
* Determines the insertion position for a new interface/class declaration.
* If the given ModuleDecl has a MainBlock the insertion point is right
* before the main block. Otherwise, the insertion point is at the end of
* the ModuleDecl.
*
* @param d
* The target document
* @param astNode
* The target ModuleDecl
* @return the insertion position of the new class or interface.
* @throws BadLocationException
* if the location of the module declaration or the respective
* main block could not be found
*/
public static int getInsertionPosition(IDocument d, InternalASTNode<ModuleDecl> astNode) throws BadLocationException {
Assert.isNotNull(d);
Assert.isNotNull(astNode);
final ModuleDecl m = astNode.getASTNode();
Assert.isNotNull(m, "ModuleDecl argument is null!");
final EditorPosition position;
/* Classes and interfaces go before product lines / products in the grammar.
* We don't want to generate invalid input for the user.
*/
if (m.hasBlock()) {
MainBlock block = m.getBlock();
position = UtilityFunctions.getPosition(block);
return d.getLineOffset(position.getLinestart()) + position.getColstart();
} else {
position = UtilityFunctions.getPosition(m);
return d.getLineOffset(position.getLineend()) + position.getColend();
}
}
use of org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition in project abstools by abstools.
the class LinkHelper method getModuleDeclAtCurrentCursor.
private ModuleDecl getModuleDeclAtCurrentCursor(IFile file, AbsNature nature, ITextEditor editor) {
ModuleDecl md = null;
synchronized (nature.modelLock) {
if (nature.getCompilationUnit(file).getNumModuleDecl() > 0) {
// get start line of editor selection
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
int startLine = selection.getStartLine();
CompilationUnit cu = nature.getCompilationUnit(file);
// find the module which the selection is located in...
for (ModuleDecl m : cu.getModuleDecls()) {
EditorPosition position = UtilityFunctions.getPosition(m);
int moduleStartLine = position.getLinestart();
int moduleEndLine = position.getLineend();
if (startLine >= moduleStartLine && startLine <= moduleEndLine) {
md = m;
break;
}
}
// if no module was found or no selection was made, take the first one in the compilation unit as fallback
if (md == null) {
md = cu.getModuleDecl(0);
}
}
}
return md;
}
use of org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition in project abstools by abstools.
the class ModuleDecorator method hasModuleDeclErrors.
/**
* Determines if a module declaration has any errors
*
* @param m
* the module declaration
* @param nature
* the ABS nature
* @return TRUE if the module declaration has errors, FALSE if not or m or
* nature is null
*/
public boolean hasModuleDeclErrors(ModuleDecl m, AbsNature nature) {
synchronized (nature.modelLock) {
if (m != null) {
CompilationUnit cu = m.getCompilationUnit();
EditorPosition pos = UtilityFunctions.getPosition(m);
int startLine = pos.getLinestart();
int endLine = pos.getLineend();
List<ParserError> parserErrors = cu.getParserErrors();
SemanticConditionList list = cu.getModel().getTypeErrors();
if (checkParserErrorRange(startLine, endLine, parserErrors)) {
return true;
} else {
return checkSemanticErrorRange(list, cu, startLine, endLine, nature);
}
}
return false;
}
}
use of org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition in project abstools by abstools.
the class ABSEditor method highlightInEditor.
/**
* Highlights the given {@link ASTNode} in the editor.<br/>
* Only one ASTNode at a time can be highlighted (This is a restriction of {@link ITextEditor}}).<br/><br/>
*
* @param edit The target editor
* @param node The node that should be highlighted.
*/
void highlightInEditor(ASTNode<?> node, boolean moveCursor) {
EditorPosition pos = UtilityFunctions.getPosition(node);
if (pos != null) {
IDocument doc = getDocumentProvider().getDocument(getEditorInput());
try {
/* Calculate the position on the editor by retrieving the char offset
* of the position line and the target column in this line.
*/
int startOff = doc.getLineOffset(pos.getLinestart()) + pos.getColstart();
int endOff = doc.getLineOffset(pos.getLineend()) + pos.getColend();
if (startOff < 0) {
// TODO: This happens sometimes - new parser?
Activator.logException(new IllegalArgumentException("FIXME"));
return;
}
assert startOff > -1;
assert endOff >= startOff;
setHighlightRange(startOff, endOff - startOff, moveCursor);
} catch (BadLocationException e) {
/*
* Should not be thrown, as an ASTNode in a document must have a
* specific location.
*/
}
}
}
use of org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition in project abstools by abstools.
the class AbsHyperlinkDetector method getHyperlinks.
public static IHyperlink[] getHyperlinks(ABSEditor editor, int offset) {
try {
InternalASTNode<CompilationUnit> cu = editor.getCompilationUnit();
if (cu == null) {
return null;
}
IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
EditorPosition targetPos = null;
ASTNode<?> node;
ASTNode<?> decl;
synchronized (cu.getNature()) {
node = UtilityFunctions.getASTNodeOfOffset(doc, cu.getASTNode(), offset);
if (node == null) {
return null;
}
decl = getDecl(cu.getASTNode(), node);
targetPos = getPosition(decl);
}
if (targetPos == null) {
return null;
}
final int startOffset = getOffset(doc, node.getStartLine(), node.getStartColumn());
final int endOffset = getOffset(doc, node.getEndLine(), node.getEndColumn());
if (decl instanceof MethodSig) {
MethodSig methodSig = (MethodSig) decl;
if (methodSig.getContextDecl() instanceof InterfaceDecl) {
// decl is an interface method
Type typ = new InterfaceType((InterfaceDecl) methodSig.getContextDecl());
if (node instanceof Call) {
// in case of a call the type can be determined more exactly
Call call = (Call) node;
typ = call.getCallee().getType();
}
return new IHyperlink[] { new JumpToDeclaration(editor, startOffset, endOffset, targetPos), new JumpToImplementation(editor, startOffset, endOffset, methodSig.getName(), typ) };
} else if (methodSig.getContextDecl() instanceof ClassDecl) {
// cursor is on a class method => provide links to methods in interfaces
ClassDecl classDecl = (ClassDecl) methodSig.getContextDecl();
List<IHyperlink> links = new ArrayList<IHyperlink>();
Set<MethodSig> addedMethods = new HashSet<MethodSig>();
for (InterfaceDecl sup : classDecl.getSuperTypes()) {
MethodSig lookupMethod = sup.lookupMethod(methodSig.getName());
if (lookupMethod != null && !addedMethods.contains(lookupMethod)) {
addedMethods.add(lookupMethod);
targetPos = getPosition(lookupMethod);
String name = lookupMethod.getContextDecl().getName() + "." + lookupMethod.getName();
links.add(new JumpToDeclaration(editor, startOffset, endOffset, targetPos, name));
}
}
if (links.size() > 0) {
return links.toArray(new IHyperlink[links.size()]);
} else {
return null;
}
}
}
return new IHyperlink[] { new JumpToDeclaration(editor, startOffset, endOffset, targetPos) };
} catch (BadLocationException e) {
e.printStackTrace();
return null;
}
}
Aggregations