use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class ConvertConcatenationToGstringIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final PsiFile file = element.getContainingFile();
final int offset = editor.getCaretModel().getOffset();
final AccessToken accessToken = ReadAction.start();
final List<GrExpression> expressions;
try {
expressions = collectExpressions(file, offset);
} finally {
accessToken.finish();
}
final Document document = editor.getDocument();
if (expressions.size() == 1) {
invokeImpl(expressions.get(0), document);
} else if (!expressions.isEmpty()) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
invokeImpl(expressions.get(expressions.size() - 1), document);
return;
}
IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() {
@Override
public void pass(final GrExpression selectedValue) {
invokeImpl(selectedValue, document);
}
}, grExpression -> grExpression.getText());
}
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class RemoveUnnecessaryEscapeCharactersIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final Document document = editor.getDocument();
final TextRange range = element.getTextRange();
document.replaceString(range.getStartOffset(), range.getEndOffset(), removeUnnecessaryEscapeSymbols((GrLiteral) element));
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class TaskChangelistSupport method installSearch.
public void installSearch(EditorTextField name, final EditorTextField comment) {
Document document = name.getDocument();
final TaskAutoCompletionListProvider completionProvider = new TaskAutoCompletionListProvider(myProject);
TextFieldWithAutoCompletion.installCompletion(document, myProject, completionProvider, false);
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class CustomRegexpFilter method createOpenFileHyperlink.
protected HyperlinkInfo createOpenFileHyperlink(String fileName, final int line, int column) {
if ((fileName == null || fileName.length() == 0)) {
if (myBase != null) {
fileName = myBase.getPresentableUrl();
} else {
return null;
}
}
fileName = fileName.replace(File.separatorChar, '/');
VirtualFile file;
// try to interpret the filename as URL
if (URLUtil.containsScheme(fileName)) {
try {
file = VfsUtil.findFileByURL(new URL(fileName));
} catch (MalformedURLException e) {
file = VirtualFileManager.getInstance().findFileByUrl(VfsUtil.pathToUrl(fileName));
}
} else {
file = VfsUtil.findRelativeFile(fileName, myBase);
}
if (file == null) {
//noinspection ConstantConditions
return null;
}
final FileType fileType = file.getFileType();
if (fileType != null && column > 0) {
final Document document = FileDocumentManager.getInstance().getDocument(file);
final int start = document.getLineStartOffset(line);
final int max = document.getLineEndOffset(line);
final int tabSize = CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings().getTabSize(fileType);
column = EditorUtil.calcColumnNumber(null, document.getCharsSequence(), start, Math.min(start + column, max), tabSize);
}
return new OpenFileHyperlinkInfo(myProject, file, line, column);
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PyStatementMover method moveInOut.
private static int moveInOut(@NotNull final MyLineRange toMove, @NotNull final Editor editor, @NotNull final MoveInfo info) {
boolean removePass = false;
final ScopeRange toMove2 = (ScopeRange) info.toMove2;
final PsiElement scope = toMove2.getScope();
final PsiElement anchor = toMove2.getAnchor();
final Project project = scope.getProject();
final PsiElement startElement = toMove.myStartElement;
final PsiElement endElement = toMove.myEndElement;
PsiElement parent = startElement.getParent();
if (scope instanceof PyStatementList && !(startElement == endElement && startElement instanceof PsiComment)) {
final PyStatement[] statements = ((PyStatementList) scope).getStatements();
if (statements.length == 1 && statements[0] == anchor && statements[0] instanceof PyPassStatement) {
removePass = true;
}
}
final PsiElement addedElement;
PsiElement nextSibling = startElement.getNextSibling();
if (toMove2.isAddBefore()) {
PsiElement tmp = endElement.getPrevSibling();
if (startElement != endElement && tmp != null) {
addedElement = scope.addRangeBefore(startElement, tmp, anchor);
scope.addBefore(endElement, anchor);
} else {
addedElement = scope.addBefore(endElement, anchor);
}
} else {
if (startElement != endElement && nextSibling != null) {
scope.addRangeAfter(nextSibling, endElement, anchor);
}
addedElement = scope.addAfter(startElement, anchor);
}
addPassStatement(toMove, project);
if (startElement != endElement && nextSibling != null) {
parent.deleteChildRange(nextSibling, endElement);
}
startElement.delete();
final int addedElementLine = editor.getDocument().getLineNumber(addedElement.getTextOffset());
final PsiFile file = scope.getContainingFile();
adjustLineIndents(editor, scope, project, addedElement, toMove.size);
if (removePass) {
ApplicationManager.getApplication().runWriteAction(() -> {
final Document document = editor.getDocument();
final int lineNumber = document.getLineNumber(anchor.getTextOffset());
final int endOffset = document.getLineCount() <= lineNumber + 1 ? document.getLineEndOffset(lineNumber) : document.getLineStartOffset(lineNumber + 1);
document.deleteString(document.getLineStartOffset(lineNumber), endOffset);
PsiDocumentManager.getInstance(startElement.getProject()).commitAllDocuments();
});
}
int offset = addedElement.getTextRange().getStartOffset();
int newLine = editor.getDocument().getLineNumber(offset);
if (newLine != addedElementLine && !removePass) {
// PsiComment gets broken after adjust indent
PsiElement psiElement = PyUtil.findNonWhitespaceAtOffset(file, editor.getDocument().getLineEndOffset(addedElementLine) - 1);
if (psiElement != null) {
psiElement = getCommentOrStatement(editor.getDocument(), psiElement);
offset = psiElement.getTextRange().getStartOffset();
}
}
return offset;
}
Aggregations