use of com.intellij.openapi.editor.RangeMarker 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.RangeMarker in project intellij-community by JetBrains.
the class CreateNSDeclarationIntentionFix method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
final PsiElement element = myElement.retrieve();
if (element == null)
return;
XmlFile xmlFile = getFile();
final String[] namespaces = ArrayUtil.toStringArray(getNamespaces(element, xmlFile));
runActionOverSeveralAttributeValuesAfterLettingUserSelectTheNeededOne(namespaces, project, new StringToAttributeProcessor() {
@Override
public void doSomethingWithGivenStringToProduceXmlAttributeNowPlease(@NotNull final String namespace) throws IncorrectOperationException {
String prefix = myNamespacePrefix;
if (StringUtil.isEmpty(prefix)) {
final XmlFile xmlFile = XmlExtension.getExtension(file).getContainingFile(element);
prefix = ExtendedTagInsertHandler.getPrefixByNamespace(xmlFile, namespace);
if (StringUtil.isNotEmpty(prefix)) {
// namespace already declared
ExtendedTagInsertHandler.qualifyWithPrefix(prefix, element);
return;
} else {
prefix = ExtendedTagInsertHandler.suggestPrefix(xmlFile, namespace);
if (!StringUtil.isEmpty(prefix)) {
ExtendedTagInsertHandler.qualifyWithPrefix(prefix, element);
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
}
}
}
final int offset = editor.getCaretModel().getOffset();
final RangeMarker marker = editor.getDocument().createRangeMarker(offset, offset);
final XmlNamespaceHelper helper = XmlNamespaceHelper.getHelper(file);
helper.insertNamespaceDeclaration((XmlFile) file, editor, Collections.singleton(namespace), prefix, new XmlNamespaceHelper.Runner<String, IncorrectOperationException>() {
@Override
public void run(final String param) throws IncorrectOperationException {
if (!namespace.isEmpty()) {
editor.getCaretModel().moveToOffset(marker.getStartOffset());
}
}
});
}
}, getTitle(), this, editor);
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class ChainCompletionNewVariableLookupElement method handleInsert.
@Override
public void handleInsert(final InsertionContext context) {
final RangeMarker rangeMarker = context.getDocument().createRangeMarker(context.getStartOffset(), context.getStartOffset());
getDelegate().handleInsert(context);
final PsiFile file = context.getFile();
((PsiJavaFile) file).importClass(myPsiClass);
final PsiElement caretElement = file.findElementAt(context.getEditor().getCaretModel().getOffset());
if (caretElement == null) {
log.error("element on caret position MUST BE not null");
return;
}
final PsiStatement statement = (PsiStatement) caretElement.getPrevSibling();
final PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(statement, PsiCodeBlock.class);
if (codeBlock == null) {
log.error("code block MUST BE not null");
return;
}
final Project project = context.getProject();
final Ref<PsiElement> insertedStatementRef = Ref.create();
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
context.commitDocument();
final PsiStatement statementFromText = elementFactory.createStatementFromText(String.format("%s %s = null;", myPsiClass.getName(), myNewVarName), null);
insertedStatementRef.set(codeBlock.addBefore(statementFromText, statement));
final PsiLiteralExpression nullKeyword = findNull(insertedStatementRef.get());
PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument());
context.getDocument().insertString(rangeMarker.getStartOffset(), myNewVarName + ".");
context.commitDocument();
final int offset = nullKeyword.getTextOffset();
final int endOffset = offset + nullKeyword.getTextLength();
context.getEditor().getSelectionModel().setSelection(offset, endOffset);
context.getEditor().getCaretModel().moveToOffset(offset);
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class OffsetMap method getOffset.
/**
* @param key key
* @return offset An offset registered earlier with this key.
* -1 if offset wasn't registered or became invalidated due to document changes
*/
public int getOffset(OffsetKey key) {
synchronized (myMap) {
final RangeMarker marker = myMap.get(key);
if (marker == null)
throw new IllegalArgumentException("Offset " + key + " is not registered");
if (!marker.isValid()) {
removeOffset(key);
throw new IllegalStateException("Offset " + key + " is invalid: " + marker);
}
final int endOffset = marker.getEndOffset();
if (marker.getStartOffset() != endOffset) {
saveOffset(key, endOffset, false);
}
return endOffset;
}
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class DaemonCodeAnalyzerImpl method isOffsetInsideHighlightInfo.
private static boolean isOffsetInsideHighlightInfo(int offset, @NotNull HighlightInfo info, boolean includeFixRange) {
RangeHighlighterEx highlighter = info.getHighlighter();
if (highlighter == null || !highlighter.isValid())
return false;
int startOffset = highlighter.getStartOffset();
int endOffset = highlighter.getEndOffset();
if (startOffset <= offset && offset <= endOffset) {
return true;
}
if (!includeFixRange)
return false;
RangeMarker fixMarker = info.fixMarker;
if (fixMarker != null) {
// null means its range is the same as highlighter
if (!fixMarker.isValid())
return false;
startOffset = fixMarker.getStartOffset();
endOffset = fixMarker.getEndOffset();
return startOffset <= offset && offset <= endOffset;
}
return false;
}
Aggregations