use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.
the class ExtendedTagInsertHandler method handleInsert.
@Override
public void handleInsert(final InsertionContext context, final LookupElement item) {
final XmlFile contextFile = (XmlFile) context.getFile();
final XmlExtension extension = XmlExtension.getExtension(contextFile);
final XmlFile file = extension.getContainingFile(contextFile);
final Project project = context.getProject();
assert file != null;
final PsiElement psiElement = file.findElementAt(context.getStartOffset());
assert psiElement != null;
if (isNamespaceBound(psiElement)) {
doDefault(context, item);
return;
}
final Editor editor = context.getEditor();
final Document document = editor.getDocument();
PsiDocumentManager.getInstance(project).commitDocument(document);
final int caretOffset = editor.getCaretModel().getOffset();
final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
caretMarker.setGreedyToRight(true);
final XmlNamespaceHelper.Runner<String, IncorrectOperationException> runAfter = new XmlNamespaceHelper.Runner<String, IncorrectOperationException>() {
@Override
public void run(final String namespacePrefix) {
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiElement element = file.findElementAt(context.getStartOffset());
if (element != null) {
qualifyWithPrefix(namespacePrefix, element, document);
PsiDocumentManager.getInstance(project).commitDocument(document);
}
editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
doDefault(context, item);
}
};
try {
final String prefixByNamespace = getPrefixByNamespace(file, myNamespace);
if (myNamespacePrefix != null || StringUtil.isEmpty(prefixByNamespace)) {
final String nsPrefix = myNamespacePrefix == null ? suggestPrefix(file, myNamespace) : myNamespacePrefix;
XmlNamespaceHelper.getHelper(file).insertNamespaceDeclaration(file, editor, Collections.singleton(myNamespace), nsPrefix, runAfter);
FeatureUsageTracker.getInstance().triggerFeatureUsed(XmlCompletionContributor.TAG_NAME_COMPLETION_FEATURE);
} else {
// qualify && complete
runAfter.run(prefixByNamespace);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.
the class XmlAttributeInsertHandler method handleInsert.
@Override
public void handleInsert(final InsertionContext context, final LookupElement item) {
final Editor editor = context.getEditor();
final Document document = editor.getDocument();
final int caretOffset = editor.getCaretModel().getOffset();
final PsiFile file = context.getFile();
final CharSequence chars = document.getCharsSequence();
final String quote = getAttributeQuote(HtmlUtil.hasHtml(file) || HtmlUtil.supportsXmlTypedHandlers(file));
final boolean insertQuotes = WebEditorOptions.getInstance().isInsertQuotesForAttributeValue() && StringUtil.isNotEmpty(quote);
final boolean hasQuotes = CharArrayUtil.regionMatches(chars, caretOffset, "=\"") || CharArrayUtil.regionMatches(chars, caretOffset, "='");
if (!hasQuotes) {
PsiElement fileContext = file.getContext();
String toInsert = null;
if (fileContext != null) {
if (fileContext.getText().startsWith("\""))
toInsert = "=''";
if (fileContext.getText().startsWith("\'"))
toInsert = "=\"\"";
}
if (toInsert == null) {
toInsert = "=" + quote + quote;
}
if (!insertQuotes)
toInsert = "=";
if (caretOffset >= document.getTextLength() || "/> \n\t\r".indexOf(document.getCharsSequence().charAt(caretOffset)) < 0) {
document.insertString(caretOffset, toInsert + " ");
} else {
document.insertString(caretOffset, toInsert);
}
if ('=' == context.getCompletionChar()) {
// IDEA-19449
context.setAddCompletionChar(false);
}
}
editor.getCaretModel().moveToOffset(caretOffset + (insertQuotes || hasQuotes ? 2 : 1));
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
AutoPopupController.getInstance(editor.getProject()).scheduleAutoPopup(editor);
if (myNamespaceToInsert != null && file instanceof XmlFile) {
final PsiElement element = file.findElementAt(context.getStartOffset());
final XmlTag tag = element != null ? PsiTreeUtil.getParentOfType(element, XmlTag.class) : null;
if (tag != null) {
String prefix = ExtendedTagInsertHandler.suggestPrefix((XmlFile) file, myNamespaceToInsert);
if (prefix != null) {
prefix = makePrefixUnique(prefix, tag);
final XmlNamespaceHelper helper = XmlNamespaceHelper.getHelper(context.getFile());
if (helper != null) {
final Project project = context.getProject();
PsiDocumentManager.getInstance(project).commitDocument(document);
qualifyWithPrefix(prefix, element);
helper.insertNamespaceDeclaration((XmlFile) file, editor, Collections.singleton(myNamespaceToInsert), prefix, null);
}
}
}
}
}
use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.
the class XmlClosingTagInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
Editor editor = context.getEditor();
Document document = editor.getDocument();
Project project = context.getProject();
if (item instanceof LookupElementDecorator) {
((LookupElementDecorator) item).getDelegate().handleInsert(context);
}
PsiDocumentManager.getInstance(project).commitDocument(document);
int lineOffset = document.getLineStartOffset(document.getLineNumber(editor.getCaretModel().getOffset()));
CodeStyleManager.getInstance(project).adjustLineIndent(document, lineOffset);
}
use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.
the class CreateNSDeclarationIntentionFix method applyFix.
@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PsiFile containingFile = descriptor.getPsiElement().getContainingFile();
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
final PsiFile file = editor != null ? PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()) : null;
if (file == null || !Comparing.equal(file.getVirtualFile(), containingFile.getVirtualFile()))
return;
try {
invoke(project, editor, containingFile);
} catch (IncorrectOperationException ex) {
LOG.error(ex);
}
}
use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.
the class IpnbPyReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
final List<Object> variants = Lists.newArrayList();
Collections.addAll(variants, super.getVariants());
PsiFile file = myElement.getContainingFile();
if (file instanceof IpnbPyFragment) {
final IpnbFilePanel panel = ((IpnbPyFragment) file).getFilePanel();
final List<IpnbEditablePanel> panels = panel.getIpnbPanels();
for (IpnbEditablePanel editablePanel : panels) {
if (!(editablePanel instanceof IpnbCodePanel))
continue;
final Editor editor = ((IpnbCodePanel) editablePanel).getEditor();
final IpnbPyFragment psiFile = (IpnbPyFragment) PsiDocumentManager.getInstance(myElement.getProject()).getPsiFile(editor.getDocument());
if (psiFile == null)
continue;
final CompletionVariantsProcessor processor = new CompletionVariantsProcessor(myElement);
PyResolveUtil.scopeCrawlUp(processor, psiFile, null, null);
variants.addAll(getOriginalElements(processor));
}
}
return variants.toArray();
}
Aggregations