use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-community by JetBrains.
the class LanguageConsoleImpl method printWithHighlighting.
public static String printWithHighlighting(@NotNull LanguageConsoleView console, @NotNull Editor inputEditor, @NotNull TextRange textRange) {
String text;
EditorHighlighter highlighter;
if (inputEditor instanceof EditorWindow) {
PsiFile file = ((EditorWindow) inputEditor).getInjectedFile();
highlighter = HighlighterFactory.createHighlighter(file.getVirtualFile(), EditorColorsManager.getInstance().getGlobalScheme(), console.getProject());
String fullText = InjectedLanguageUtil.getUnescapedText(file, null, null);
highlighter.setText(fullText);
text = textRange.substring(fullText);
} else {
text = inputEditor.getDocument().getText(textRange);
highlighter = ((EditorEx) inputEditor).getHighlighter();
}
SyntaxHighlighter syntax = highlighter instanceof LexerEditorHighlighter ? ((LexerEditorHighlighter) highlighter).getSyntaxHighlighter() : null;
((LanguageConsoleImpl) console).doAddPromptToHistory();
if (syntax != null) {
ConsoleViewUtil.printWithHighlighting(console, text, syntax);
} else {
console.print(text, ConsoleViewContentType.USER_INPUT);
}
console.print("\n", ConsoleViewContentType.NORMAL_OUTPUT);
return text;
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-community by JetBrains.
the class BaseIndentEnterHandler method preprocessEnter.
@Override
public Result preprocessEnter(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Ref<Integer> caretOffset, @NotNull final Ref<Integer> caretAdvance, @NotNull final DataContext dataContext, final EditorActionHandler originalHandler) {
Result res = shouldSkipWithResult(file, editor, dataContext);
if (res != null) {
return res;
}
final Document document = editor.getDocument();
int caret = editor.getCaretModel().getOffset();
final int lineNumber = document.getLineNumber(caret);
final int lineStartOffset = document.getLineStartOffset(lineNumber);
final int previousLineStartOffset = lineNumber > 0 ? document.getLineStartOffset(lineNumber - 1) : lineStartOffset;
final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
final HighlighterIterator iterator = highlighter.createIterator(caret - 1);
final IElementType type = getNonWhitespaceElementType(iterator, lineStartOffset, previousLineStartOffset);
final CharSequence editorCharSequence = document.getCharsSequence();
final CharSequence lineIndent = editorCharSequence.subSequence(lineStartOffset, EditorActionUtil.findFirstNonSpaceOffsetOnTheLine(document, lineNumber));
// Enter in line comment
if (type == myLineCommentType) {
final String restString = editorCharSequence.subSequence(caret, document.getLineEndOffset(lineNumber)).toString();
if (!StringUtil.isEmptyOrSpaces(restString)) {
final String linePrefix = lineIndent + myLineCommentPrefix;
EditorModificationUtil.insertStringAtCaret(editor, "\n" + linePrefix);
editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(lineNumber + 1, linePrefix.length()));
return Result.Stop;
} else if (iterator.getStart() < lineStartOffset) {
EditorModificationUtil.insertStringAtCaret(editor, "\n" + lineIndent);
return Result.Stop;
}
}
if (!myWorksWithFormatter && LanguageFormatting.INSTANCE.forLanguage(myLanguage) != null) {
return Result.Continue;
} else {
if (myIndentTokens.contains(type)) {
final String newIndent = getNewIndent(file, document, lineIndent);
EditorModificationUtil.insertStringAtCaret(editor, "\n" + newIndent);
return Result.Stop;
}
EditorModificationUtil.insertStringAtCaret(editor, "\n" + lineIndent);
editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(lineNumber + 1, calcLogicalLength(editor, lineIndent)));
return Result.Stop;
}
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-community by JetBrains.
the class GStringBackspaceHandlerDelegate method beforeCharDeleted.
@Override
public void beforeCharDeleted(char c, PsiFile file, Editor editor) {
if (c != '{')
return;
if (!(file instanceof GroovyFile))
return;
final int offset = editor.getCaretModel().getOffset();
final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
if (offset < 1)
return;
HighlighterIterator iterator = highlighter.createIterator(offset);
if (iterator.getTokenType() != GroovyTokenTypes.mRCURLY)
return;
iterator.retreat();
if (iterator.getStart() < 1 || iterator.getTokenType() != GroovyTokenTypes.mLCURLY)
return;
editor.getDocument().deleteString(offset, offset + 1);
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-community by JetBrains.
the class TodoTreeBuilder method getHighlighter.
/**
* @return {@code SelectInEditorManager} for the specified {@code psiFile}. Highlighters are
* lazy created and initialized.
*/
public EditorHighlighter getHighlighter(PsiFile psiFile, Document document) {
VirtualFile file = psiFile.getVirtualFile();
if (myFile2Highlighter.containsKey(file)) {
return myFile2Highlighter.get(file);
} else {
EditorHighlighter highlighter = HighlighterFactory.createHighlighter(UsageTreeColorsScheme.getInstance().getScheme(), file.getName(), myProject);
highlighter.setText(document.getCharsSequence());
myFile2Highlighter.put(file, highlighter);
return highlighter;
}
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-community by JetBrains.
the class RParenthTailType method getExistingRParenthOffset.
private static int getExistingRParenthOffset(final Editor editor, final int tailOffset) {
final Document document = editor.getDocument();
if (tailOffset >= document.getTextLength())
return -1;
final CharSequence charsSequence = document.getCharsSequence();
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
int existingRParenthOffset = -1;
for (HighlighterIterator iterator = highlighter.createIterator(tailOffset); !iterator.atEnd(); iterator.advance()) {
final IElementType tokenType = iterator.getTokenType();
if ((!(tokenType instanceof IJavaElementType) || !ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(tokenType)) && tokenType != TokenType.WHITE_SPACE) {
final int start = iterator.getStart();
if (iterator.getEnd() == start + 1 && ')' == charsSequence.charAt(start)) {
existingRParenthOffset = start;
}
break;
}
}
if (existingRParenthOffset >= 0) {
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(editor.getProject());
psiDocumentManager.commitDocument(document);
TextRange range = getRangeToCheckParensBalance(psiDocumentManager.getPsiFile(document), document, editor.getCaretModel().getOffset());
int balance = calcParensBalance(document, highlighter, range.getStartOffset(), range.getEndOffset());
if (balance > 0) {
return -1;
}
}
return existingRParenthOffset;
}
Aggregations