use of com.intellij.injected.editor.EditorWindow 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.injected.editor.EditorWindow in project intellij-community by JetBrains.
the class BackspaceHandler method isOffsetInsideInjected.
private static boolean isOffsetInsideInjected(Editor injectedEditor, int injectedOffset) {
if (injectedOffset == 0 || injectedOffset >= injectedEditor.getDocument().getTextLength()) {
return false;
}
PsiFile injectedFile = ((EditorWindow) injectedEditor).getInjectedFile();
InjectedLanguageManager ilm = InjectedLanguageManager.getInstance(injectedFile.getProject());
TextRange rangeToEdit = new TextRange(injectedOffset - 1, injectedOffset + 1);
List<TextRange> editables = ilm.intersectWithAllEditableFragments(injectedFile, rangeToEdit);
return editables.size() == 1 && editables.get(0).equals(rangeToEdit);
}
use of com.intellij.injected.editor.EditorWindow in project intellij-community by JetBrains.
the class SelectionQuotingTypedHandler method beforeSelectionRemoved.
@Override
public Result beforeSelectionRemoved(char c, Project project, Editor editor, PsiFile file) {
SelectionModel selectionModel = editor.getSelectionModel();
if (CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED && selectionModel.hasSelection() && isDelimiter(c)) {
String selectedText = selectionModel.getSelectedText();
if (!StringUtil.isEmpty(selectedText)) {
final int selectionStart = selectionModel.getSelectionStart();
final int selectionEnd = selectionModel.getSelectionEnd();
if (selectedText.length() > 1) {
final char firstChar = selectedText.charAt(0);
final char lastChar = selectedText.charAt(selectedText.length() - 1);
if (isSimilarDelimiters(firstChar, c) && lastChar == getMatchingDelimiter(firstChar) && (isQuote(firstChar) || firstChar != c) && !shouldSkipReplacementOfQuotesOrBraces(file, editor, selectedText, c) && selectedText.indexOf(lastChar, 1) == selectedText.length() - 1) {
selectedText = selectedText.substring(1, selectedText.length() - 1);
}
}
final int caretOffset = selectionModel.getSelectionStart();
final char c2 = getMatchingDelimiter(c);
final String newText = String.valueOf(c) + selectedText + c2;
boolean ltrSelection = selectionModel.getLeadSelectionOffset() != selectionModel.getSelectionEnd();
boolean restoreStickySelection = editor instanceof EditorEx && ((EditorEx) editor).isStickySelection();
selectionModel.removeSelection();
editor.getDocument().replaceString(selectionStart, selectionEnd, newText);
TextRange replacedTextRange = Registry.is("editor.smarterSelectionQuoting") ? new TextRange(caretOffset + 1, caretOffset + newText.length() - 1) : new TextRange(caretOffset, caretOffset + newText.length());
// selection is removed here
if (replacedTextRange.getEndOffset() <= editor.getDocument().getTextLength()) {
if (restoreStickySelection) {
EditorEx editorEx = (EditorEx) editor;
CaretModel caretModel = editorEx.getCaretModel();
caretModel.moveToOffset(ltrSelection ? replacedTextRange.getStartOffset() : replacedTextRange.getEndOffset());
editorEx.setStickySelection(true);
caretModel.moveToOffset(ltrSelection ? replacedTextRange.getEndOffset() : replacedTextRange.getStartOffset());
} else {
if (ltrSelection || editor instanceof EditorWindow) {
editor.getSelectionModel().setSelection(replacedTextRange.getStartOffset(), replacedTextRange.getEndOffset());
} else {
editor.getSelectionModel().setSelection(replacedTextRange.getEndOffset(), replacedTextRange.getStartOffset());
}
if (Registry.is("editor.smarterSelectionQuoting")) {
editor.getCaretModel().moveToOffset(ltrSelection ? replacedTextRange.getEndOffset() : replacedTextRange.getStartOffset());
}
}
}
return Result.STOP;
}
}
return super.beforeSelectionRemoved(c, project, editor, file);
}
use of com.intellij.injected.editor.EditorWindow in project intellij-community by JetBrains.
the class Xslt2RefactoringTest method doExtractFunction.
private void doExtractFunction() throws Throwable {
myFixture.configureByFile(getTestFileName() + ".xsl");
final Editor editor = myFixture.getEditor();
assertTrue("Selection required", editor.getSelectionModel().hasSelection());
editor.getCaretModel().moveToOffset(editor.getSelectionModel().getSelectionStart());
final XsltExtractFunctionAction action = new XsltExtractFunctionAction() {
@Override
protected RefactoringOptions getSettings(XPathExpression expression, Set<XPathExpression> matchingExpressions) {
return new RefactoringOptions() {
@Override
public boolean isCanceled() {
return false;
}
@Override
public String getName() {
return "f:foo";
}
};
}
};
final PsiFile file = InjectedLanguageUtil.findInjectedPsiNoCommit(myFixture.getFile(), editor.getCaretModel().getOffset());
final Editor editorWindow = InjectedLanguageUtil.getInjectedEditorForInjectedFile(editor, file);
assertTrue(editorWindow instanceof EditorWindow);
action.invoke(myFixture.getProject(), editorWindow, file, null);
myFixture.checkResultByFile(getTestFileName() + "_after.xsl");
}
use of com.intellij.injected.editor.EditorWindow in project intellij-community by JetBrains.
the class VariableInplaceRenamer method restoreSelection.
@Override
protected void restoreSelection() {
if (mySelectedRange != null) {
Editor editor = InjectedLanguageUtil.getTopLevelEditor(myEditor);
TextRange selectedRange;
if (myEditor instanceof EditorWindow) {
PsiFile injected = ((EditorWindow) myEditor).getInjectedFile();
selectedRange = InjectedLanguageManager.getInstance(editor.getProject()).injectedToHost(injected, mySelectedRange);
} else {
selectedRange = mySelectedRange;
}
TemplateState state = TemplateManagerImpl.getTemplateState(editor);
if (state != null) {
for (int i = 0; i < state.getSegmentsCount(); i++) {
final TextRange segmentRange = state.getSegmentRange(i);
TextRange intersection = segmentRange.intersection(selectedRange);
if (intersection != null) {
editor.getSelectionModel().setSelection(intersection.getStartOffset(), intersection.getEndOffset());
return;
}
}
}
myEditor.getSelectionModel().setSelection(mySelectedRange.getStartOffset(), mySelectedRange.getEndOffset());
} else if (!shouldSelectAll()) {
myEditor.getSelectionModel().removeSelection();
}
}
Aggregations