use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PyConsoleUtil method createTabCompletionAction.
public static AnAction createTabCompletionAction(PythonConsoleView consoleView) {
final AnAction runCompletions = new AnAction() {
@Override
public void actionPerformed(AnActionEvent e) {
Editor editor = consoleView.getConsoleEditor();
if (LookupManager.getActiveLookup(editor) != null) {
AnAction replace = ActionManager.getInstance().getAction(IdeActions.ACTION_CHOOSE_LOOKUP_ITEM_REPLACE);
ActionUtil.performActionDumbAware(replace, e);
return;
}
AnAction completionAction = ActionManager.getInstance().getAction(IdeActions.ACTION_CODE_COMPLETION);
if (completionAction != null) {
ActionUtil.performActionDumbAware(completionAction, e);
}
}
@Override
public void update(AnActionEvent e) {
Editor editor = consoleView.getConsoleEditor();
if (LookupManager.getActiveLookup(editor) != null) {
e.getPresentation().setEnabled(false);
}
int offset = editor.getCaretModel().getOffset();
Document document = editor.getDocument();
int lineStart = document.getLineStartOffset(document.getLineNumber(offset));
String textToCursor = document.getText(new TextRange(lineStart, offset));
e.getPresentation().setEnabled(!CharMatcher.WHITESPACE.matchesAllOf(textToCursor));
}
};
runCompletions.registerCustomShortcutSet(KeyEvent.VK_TAB, 0, consoleView.getConsoleEditor().getComponent());
runCompletions.getTemplatePresentation().setVisible(false);
return runCompletions;
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PySmartStepIntoHandler method computeSmartStepVariants.
@Override
@NotNull
public List<PySmartStepIntoVariant> computeSmartStepVariants(@NotNull XSourcePosition position) {
final Document document = FileDocumentManager.getInstance().getDocument(position.getFile());
final List<PySmartStepIntoVariant> variants = Lists.newArrayList();
final Set<PyCallExpression> visitedCalls = Sets.newHashSet();
final int line = position.getLine();
XDebuggerUtil.getInstance().iterateLine(mySession.getProject(), document, line, psiElement -> {
addVariants(document, line, psiElement, variants, visitedCalls);
return true;
});
return variants;
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PydevConsoleReference method getVariants.
@NotNull
public Object[] getVariants() {
Map<String, LookupElement> variants = Maps.newHashMap();
try {
final List<PydevCompletionVariant> completions = myCommunication.getCompletions(getText(), myPrefix);
for (PydevCompletionVariant completion : completions) {
final PsiManager manager = myElement.getManager();
final String name = completion.getName();
final int type = completion.getType();
LookupElementBuilder builder = LookupElementBuilder.create(new PydevConsoleElement(manager, name, completion.getDescription())).withIcon(PyCodeCompletionImages.getImageForType(type));
String args = completion.getArgs();
if (args.equals("(%)")) {
builder.withPresentableText("%" + completion.getName());
builder = builder.withInsertHandler(new InsertHandler<LookupElement>() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
final Editor editor = context.getEditor();
final Document document = editor.getDocument();
int offset = context.getStartOffset();
if (offset == 0 || !"%".equals(document.getText(TextRange.from(offset - 1, 1)))) {
document.insertString(offset, "%");
}
}
});
args = "";
} else if (!StringUtil.isEmptyOrSpaces(args)) {
builder = builder.withTailText(args);
}
// Set function insert handler
if (type == IToken.TYPE_FUNCTION || args.endsWith(")")) {
builder = builder.withInsertHandler(ParenthesesInsertHandler.WITH_PARAMETERS);
}
variants.put(name, builder);
}
} catch (Exception e) {
//LOG.error(e);
}
return variants.values().toArray();
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class RemoveExtraClosingTagIntentionAction method doFix.
private static void doFix(@NotNull final PsiElement element) throws IncorrectOperationException {
final XmlToken endNameToken = (XmlToken) element;
final PsiElement tagElement = endNameToken.getParent();
if (!(tagElement instanceof XmlTag) && !(tagElement instanceof PsiErrorElement))
return;
if (tagElement instanceof PsiErrorElement) {
tagElement.delete();
} else {
final ASTNode astNode = tagElement.getNode();
if (astNode != null) {
final ASTNode endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(astNode);
if (endTagStart != null) {
final Document document = PsiDocumentManager.getInstance(element.getProject()).getDocument(tagElement.getContainingFile());
if (document != null) {
document.deleteString(endTagStart.getStartOffset(), tagElement.getLastChild().getTextRange().getEndOffset());
}
}
}
}
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class RenameTagBeginOrEndIntentionAction method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
final int offset = editor.getCaretModel().getOffset();
PsiElement psiElement = file.findElementAt(offset);
if (psiElement == null)
return;
if (psiElement instanceof PsiWhiteSpace)
psiElement = PsiTreeUtil.prevLeaf(psiElement);
if (psiElement instanceof XmlToken) {
final IElementType tokenType = ((XmlToken) psiElement).getTokenType();
if (tokenType != XmlTokenType.XML_NAME) {
if (tokenType == XmlTokenType.XML_TAG_END) {
psiElement = psiElement.getPrevSibling();
if (psiElement == null)
return;
}
}
PsiElement target = null;
final String text = psiElement.getText();
if (!myTargetName.equals(text)) {
target = psiElement;
} else {
// we're in the other
target = findOtherSide(psiElement, myStart);
}
if (target != null) {
final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
if (document != null) {
final TextRange textRange = target.getTextRange();
document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), myTargetName);
}
}
}
}
Aggregations