use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class YAMLQuotedTextImpl method getContentRanges.
@NotNull
@Override
public List<TextRange> getContentRanges() {
final ASTNode firstContentNode = getFirstContentNode();
if (firstContentNode == null) {
return Collections.emptyList();
}
List<TextRange> result = new ArrayList<>();
TextRange contentRange = TextRange.create(firstContentNode.getStartOffset(), getTextRange().getEndOffset()).shiftRight(-getTextRange().getStartOffset());
final List<String> lines = StringUtil.split(contentRange.substring(getText()), "\n", true, false);
// First line has opening quote
int cumulativeOffset = contentRange.getStartOffset();
for (int i = 0; i < lines.size(); ++i) {
final String line = lines.get(i);
int lineStart = 0;
int lineEnd = line.length();
if (i == 0) {
lineStart++;
} else {
while (lineStart < line.length() && YAMLGrammarCharUtil.isSpaceLike(line.charAt(lineStart))) {
lineStart++;
}
}
if (i == lines.size() - 1) {
// Last line has closing quote
lineEnd--;
} else {
while (lineEnd > lineStart && YAMLGrammarCharUtil.isSpaceLike(line.charAt(lineEnd - 1))) {
lineEnd--;
}
}
result.add(TextRange.create(lineStart, lineEnd).shiftRight(cumulativeOffset));
cumulativeOffset += line.length() + 1;
}
return result;
}
use of com.intellij.openapi.util.TextRange 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.util.TextRange in project intellij-community by JetBrains.
the class PyTestCase method doPerformFormatting.
private void doPerformFormatting() throws IncorrectOperationException {
final PsiFile file = myFixture.getFile();
final TextRange myTextRange = file.getTextRange();
CodeStyleManager.getInstance(myFixture.getProject()).reformatText(file, myTextRange.getStartOffset(), myTextRange.getEndOffset());
}
use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.
the class SpellCheckerDictionaryGenerator method processLeafsNames.
protected void processLeafsNames(@NotNull final PsiElement leafElement, @NotNull final HashSet<String> seenNames) {
final Language language = leafElement.getLanguage();
SpellCheckingInspection.tokenize(leafElement, language, new TokenConsumer() {
@Override
public void consumeToken(PsiElement element, final String text, boolean useRename, int offset, TextRange rangeToCheck, Splitter splitter) {
splitter.split(text, rangeToCheck, textRange -> {
final String word = textRange.substring(text);
addSeenWord(seenNames, word, language);
});
}
});
}
use of com.intellij.openapi.util.TextRange 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