use of com.intellij.openapi.util.TextRange in project buck by facebook.
the class BuckAnnotator method annotateErrors.
private void annotateErrors(PsiElement psiElement, AnnotationHolder annotationHolder) {
BuckValue value = PsiTreeUtil.getParentOfType(psiElement, BuckValue.class);
if (value == null) {
return;
}
final Project project = psiElement.getProject();
if (project == null) {
return;
}
String target = psiElement.getText();
if (target.matches("\".*\"") || target.matches("'.*'")) {
target = target.substring(1, target.length() - 1);
} else {
return;
}
if (!BuckBuildUtil.isValidAbsoluteTarget(target)) {
return;
}
VirtualFile buckDir = project.getBaseDir().findFileByRelativePath(BuckBuildUtil.extractAbsoluteTarget(target));
VirtualFile targetBuckFile = buckDir != null ? buckDir.findChild("BUCK") : null;
if (targetBuckFile == null) {
TextRange range = new TextRange(psiElement.getTextRange().getStartOffset(), psiElement.getTextRange().getEndOffset());
annotationHolder.createErrorAnnotation(range, ANNOTATOR_ERROR_CANNOT_LOCATE_TARGET);
project.getMessageBus().syncPublisher(IntellijBuckAction.EVENT).consume(this.getClass().toString());
}
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class GroovyEnterHandler method handleInString.
private static boolean handleInString(Editor editor, int caretOffset, DataContext dataContext, EditorActionHandler originalHandler) {
Project project = CommonDataKeys.PROJECT.getData(dataContext);
if (project == null)
return false;
final VirtualFile vfile = FileDocumentManager.getInstance().getFile(editor.getDocument());
assert vfile != null;
PsiFile file = PsiManager.getInstance(project).findFile(vfile);
Document document = editor.getDocument();
String fileText = document.getText();
if (fileText.length() == caretOffset)
return false;
if (!checkStringApplicable(editor, caretOffset))
return false;
if (file == null)
return false;
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiElement stringElement = inferStringPair(file, caretOffset);
if (stringElement == null)
return false;
ASTNode node = stringElement.getNode();
final IElementType nodeElementType = node.getElementType();
boolean isInsertIndent = isInsertIndent(caretOffset, stringElement.getTextRange().getStartOffset(), fileText);
// For simple String literals like 'abc'
CaretModel caretModel = editor.getCaretModel();
if (nodeElementType == GroovyTokenTypes.mSTRING_LITERAL) {
if (isSingleQuoteString(stringElement)) {
//the case of print '\<caret>'
if (isSlashBeforeCaret(caretOffset, fileText)) {
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else if (stringElement.getParent() instanceof GrReferenceExpression) {
TextRange range = stringElement.getTextRange();
convertEndToMultiline(range.getEndOffset(), document, fileText, '\'');
document.insertString(range.getStartOffset(), "''");
caretModel.moveToOffset(caretOffset + 2);
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else {
EditorModificationUtil.insertStringAtCaret(editor, "'+");
originalHandler.execute(editor, dataContext);
EditorModificationUtil.insertStringAtCaret(editor, "'");
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
}
} else {
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
}
return true;
}
if (GSTRING_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && GSTRING_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.GSTRING) {
PsiElement parent = stringElement.getParent();
if (nodeElementType == GroovyTokenTypes.mGSTRING_LITERAL) {
parent = stringElement;
} else {
while (parent != null && !(parent instanceof GrLiteral)) {
parent = parent.getParent();
}
}
if (parent == null)
return false;
if (isDoubleQuotedString(parent)) {
PsiElement exprSibling = stringElement.getNextSibling();
boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
if (rightFromDollar)
caretOffset--;
TextRange parentRange = parent.getTextRange();
if (rightFromDollar || parent.getParent() instanceof GrReferenceExpression) {
convertEndToMultiline(parent.getTextRange().getEndOffset(), document, fileText, '"');
document.insertString(parentRange.getStartOffset(), "\"\"");
caretModel.moveToOffset(caretOffset + 2);
EditorModificationUtil.insertStringAtCaret(editor, "\n");
if (rightFromDollar) {
caretModel.moveCaretRelatively(1, 0, false, false, true);
}
} else if (isSlashBeforeCaret(caretOffset, fileText)) {
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else {
EditorModificationUtil.insertStringAtCaret(editor, "\"+");
originalHandler.execute(editor, dataContext);
EditorModificationUtil.insertStringAtCaret(editor, "\"");
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
}
} else {
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
}
return true;
}
if (REGEX_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && REGEX_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.REGEX) {
PsiElement parent = stringElement.getParent();
if (nodeElementType == GroovyTokenTypes.mREGEX_LITERAL || nodeElementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
parent = stringElement;
} else {
while (parent != null && !(parent instanceof GrLiteral)) {
parent = parent.getParent();
}
}
if (parent == null || parent.getLastChild() instanceof PsiErrorElement)
return false;
PsiElement exprSibling = stringElement.getNextSibling();
boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
if (rightFromDollar) {
caretModel.moveToOffset(caretOffset - 1);
}
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
if (rightFromDollar) {
caretModel.moveCaretRelatively(1, 0, false, false, true);
}
return true;
}
return false;
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class GrExecuteCommandAction method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
final Project project = e.getProject();
final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
if (project == null || editor == null || virtualFile == null)
return;
FileDocumentManager.getInstance().saveAllDocuments();
final Document document = editor.getDocument();
final TextRange selectedRange = EditorUtil.getSelectionInAnyMode(editor);
final String command = (selectedRange.isEmpty() ? document.getText() : document.getText(selectedRange));
final GroovyConsole existingConsole = virtualFile.getUserData(GroovyConsole.GROOVY_CONSOLE);
if (existingConsole == null) {
GroovyConsole.getOrCreateConsole(project, virtualFile, console -> console.execute(command));
} else {
existingConsole.execute(command);
}
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class GroovyMembersWithDocSelectioner method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
final GrDocCommentOwner owner;
final GrDocComment doc;
if (e instanceof GrDocComment) {
doc = (GrDocComment) e;
owner = GrDocCommentUtil.findDocOwner(doc);
} else {
owner = (GrDocCommentOwner) e;
doc = GrDocCommentUtil.findDocComment(owner);
}
if (doc == null || owner == null)
return Collections.emptyList();
final TextRange range = new TextRange(doc.getTextRange().getStartOffset(), owner.getTextRange().getEndOffset());
return ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, range, true);
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class GroovyStatementSelectioner method inferBlockRange.
private static TextRange inferBlockRange(PsiElement first, PsiElement last) {
while (true) {
PsiElement prev = first.getPrevSibling();
prev = skipWhitespaceBack(prev);
if (isOneLineFeed(prev))
prev = prev.getPrevSibling();
prev = skipWhitespaceBack(prev);
if (prev != null && prev.getNode().getElementType() == GroovyTokenTypes.mSEMI || prev instanceof GrStatement) {
first = prev;
} else {
break;
}
}
while (true) {
PsiElement next = last.getNextSibling();
next = skipWhitespacesForward(next);
if (isOneLineFeed(next))
next = next.getNextSibling();
next = skipWhitespacesForward(next);
if (next != null && next.getNode().getElementType() == GroovyTokenTypes.mSEMI || next instanceof GrStatement) {
last = next;
} else {
break;
}
}
return new TextRange(first.getTextRange().getStartOffset(), last.getTextRange().getEndOffset());
}
Aggregations