Search in sources :

Example 11 with DocumentImpl

use of com.intellij.openapi.editor.impl.DocumentImpl in project intellij-plugins by JetBrains.

the class CfmlTypedHandlerTest method testEditing.

public void testEditing() throws Throwable {
    @NonNls final String s1 = "<table bgcolor=\"#FFFFFF\"><cfoutput>\n" + "  <div id=\"#bColumn2";
    String s2 = "\" />\n" + "</cfoutput></table>";
    String s = s1 + s2;
    final Document doc = new DocumentImpl(s);
    EditorEx editor = (EditorEx) EditorFactory.getInstance().createEditor(doc);
    try {
        EditorHighlighter highlighter = HighlighterFactory.createHighlighter(getProject(), CfmlFileType.INSTANCE);
        editor.setHighlighter(highlighter);
        CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> doc.insertString(s1.length(), "#")), "", null);
        List tokensAfterUpdate = getAllTokens(highlighter);
        highlighter = HighlighterFactory.createHighlighter(getProject(), CfmlFileType.INSTANCE);
        editor.setHighlighter(highlighter);
        List tokensWithoutUpdate = getAllTokens(highlighter);
        TestCase.assertEquals(tokensWithoutUpdate, tokensAfterUpdate);
    } finally {
        EditorFactory.getInstance().releaseEditor(editor);
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) EditorEx(com.intellij.openapi.editor.ex.EditorEx) List(java.util.List) Document(com.intellij.openapi.editor.Document) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 12 with DocumentImpl

use of com.intellij.openapi.editor.impl.DocumentImpl in project intellij-plugins by JetBrains.

the class SwfHighlightingTest method testLineMarkersInSwf.

@JSTestOptions({ JSTestOption.WithFlexFacet, JSTestOption.WithLineMarkers })
public void testLineMarkersInSwf() throws Exception {
    final String testName = getTestName(false);
    myAfterCommitRunnable = () -> FlexTestUtils.addLibrary(myModule, "lib", getTestDataPath() + getBasePath() + "/", testName + ".swc", null, null);
    // actual test data is in library.swf; this file is here just because we need any file
    configureByFile("/" + testName + ".as");
    VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(getTestDataPath() + getBasePath() + "/" + testName + ".swc");
    vFile = JarFileSystem.getInstance().getJarRootForLocalFile(vFile).findChild("library.swf");
    myEditor = FileEditorManager.getInstance(myProject).openTextEditor(new OpenFileDescriptor(myProject, vFile, 0), false);
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    myFile = myPsiManager.findFile(vFile);
    ((EditorImpl) myEditor).setCaretActive();
    vFile = LocalFileSystem.getInstance().findFileByPath(getTestDataPath() + getBasePath() + "/" + testName + ".as");
    final String verificationText = StreamUtil.convertSeparators(VfsUtilCore.loadText(vFile));
    checkHighlighting(new ExpectedHighlightingData(new DocumentImpl(verificationText), false, false, true, myFile));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ExpectedHighlightingData(com.intellij.testFramework.ExpectedHighlightingData) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl) JSTestOptions(com.intellij.lang.javascript.JSTestOptions)

Example 13 with DocumentImpl

use of com.intellij.openapi.editor.impl.DocumentImpl in project intellij-community by JetBrains.

the class CopyPasteIndentProcessor method processTransferableData.

@Override
public void processTransferableData(final Project project, final Editor editor, final RangeMarker bounds, final int caretOffset, final Ref<Boolean> indented, final List<IndentTransferableData> values) {
    if (!CodeInsightSettings.getInstance().INDENT_TO_CARET_ON_PASTE) {
        return;
    }
    assert values.size() == 1;
    if (values.get(0).getOffset() == caretOffset)
        return;
    final Document document = editor.getDocument();
    final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
    if (psiFile == null || !acceptFileType(psiFile.getFileType())) {
        return;
    }
    //System.out.println("--- before indent ---\n" + document.getText());
    ApplicationManager.getApplication().runWriteAction(new Runnable() {

        @Override
        public void run() {
            final boolean useTabs = CodeStyleSettingsManager.getSettings(project).useTabCharacter(psiFile.getFileType());
            CharFilter NOT_INDENT_FILTER = new CharFilter() {

                @Override
                public boolean accept(char ch) {
                    return useTabs ? ch != '\t' : ch != ' ';
                }
            };
            String pastedText = document.getText(TextRange.create(bounds));
            int startLine = document.getLineNumber(bounds.getStartOffset());
            int endLine = document.getLineNumber(bounds.getEndOffset());
            //calculate from indent
            int fromIndent = StringUtil.findFirst(pastedText, NOT_INDENT_FILTER);
            if (fromIndent < 0)
                fromIndent = 0;
            //calculate to indent
            String initialText = document.getText(TextRange.create(0, bounds.getStartOffset())) + document.getText(TextRange.create(bounds.getEndOffset(), document.getTextLength()));
            int toIndent = 0;
            if (initialText.length() > 0) {
                final DocumentImpl initialDocument = new DocumentImpl(initialText);
                int lineNumber = initialDocument.getTextLength() > caretOffset ? initialDocument.getLineNumber(caretOffset) : initialDocument.getLineCount() - 1;
                final int offset = getLineStartSafeOffset(initialDocument, lineNumber);
                if (bounds.getStartOffset() == offset) {
                    String toString = initialDocument.getText(TextRange.create(offset, initialDocument.getLineEndOffset(lineNumber)));
                    toIndent = StringUtil.findFirst(toString, NOT_INDENT_FILTER);
                    if (toIndent < 0 && StringUtil.isEmptyOrSpaces(toString)) {
                        toIndent = toString.length();
                    } else if ((toIndent < 0 || toString.startsWith("\n")) && initialText.length() >= caretOffset) {
                        toIndent = caretOffset - offset;
                    }
                } else if (isNotApplicable(initialDocument, offset))
                    return;
                else {
                    // selection
                    startLine += 1;
                    toIndent = Math.abs(bounds.getStartOffset() - offset);
                }
            }
            // actual difference in indentation level
            int indent = toIndent - fromIndent;
            if (// indent is counted in tab units
            useTabs)
                indent *= CodeStyleSettingsManager.getSettings(project).getTabSize(psiFile.getFileType());
            // don't indent single-line text
            if (!StringUtil.startsWithWhitespace(pastedText) && !StringUtil.endsWithLineBreak(pastedText) && !(StringUtil.splitByLines(pastedText).length > 1))
                return;
            if (pastedText.endsWith("\n"))
                endLine -= 1;
            for (int i = startLine; i <= endLine; i++) {
                EditorActionUtil.indentLine(project, editor, i, indent);
            }
            indented.set(Boolean.TRUE);
        }

        private boolean isNotApplicable(DocumentImpl initialDocument, int offset) {
            return caretOffset < initialDocument.getTextLength() && !StringUtil.isEmptyOrSpaces(initialDocument.getText(TextRange.create(offset, caretOffset)));
        }
    });
//System.out.println("--- after indent ---\n" + document.getText());
}
Also used : CharFilter(com.intellij.openapi.util.text.CharFilter) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl)

Example 14 with DocumentImpl

use of com.intellij.openapi.editor.impl.DocumentImpl in project intellij-community by JetBrains.

the class AbstractJavaFormatterTest method replaceAndProcessDocument.

@NotNull
private String replaceAndProcessDocument(@NotNull final Action action, @NotNull final String text, @NotNull final PsiFile file, @Nullable final Document document) throws IncorrectOperationException {
    if (document == null) {
        fail("Don't expect the document to be null");
        return null;
    }
    if (myLineRange != null) {
        final DocumentImpl doc = new DocumentImpl(text);
        myTextRange = new TextRange(doc.getLineStartOffset(myLineRange.getStartOffset()), doc.getLineEndOffset(myLineRange.getEndOffset()));
    }
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
    CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> {
        document.replaceString(0, document.getTextLength(), text);
        manager.commitDocument(document);
        try {
            TextRange rangeToUse = myTextRange;
            if (rangeToUse == null) {
                rangeToUse = file.getTextRange();
            }
            ACTIONS.get(action).run(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
        } catch (IncorrectOperationException e) {
            assertTrue(e.getLocalizedMessage(), false);
        }
    }), action == REFORMAT ? ReformatCodeProcessor.COMMAND_NAME : "", "");
    return document.getText();
}
Also used : TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with DocumentImpl

use of com.intellij.openapi.editor.impl.DocumentImpl in project intellij-community by JetBrains.

the class CodeInsightTestFixtureImpl method checkResultWithInlays.

@Override
public void checkResultWithInlays(String text) {
    Document checkDocument = new DocumentImpl(text);
    InlayHintsChecker checker = new InlayHintsChecker(this);
    List<InlayInfo> inlayInfos = checker.extractInlays(checkDocument);
    checkResult(checkDocument.getText());
    checker.verifyInlays(inlayInfos, text);
}
Also used : InlayHintsChecker(com.intellij.testFramework.utils.inlays.InlayHintsChecker) InlayInfo(com.intellij.codeInsight.hints.InlayInfo) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl)

Aggregations

DocumentImpl (com.intellij.openapi.editor.impl.DocumentImpl)28 Document (com.intellij.openapi.editor.Document)12 NotNull (org.jetbrains.annotations.NotNull)6 RangeMarker (com.intellij.openapi.editor.RangeMarker)4 TextRange (com.intellij.openapi.util.TextRange)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiFile (com.intellij.psi.PsiFile)3 FrozenDocument (com.intellij.openapi.editor.impl.FrozenDocument)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2 FileComparisonFailure (com.intellij.rt.execution.junit.FileComparisonFailure)2 RelativePoint (com.intellij.ui.awt.RelativePoint)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 InlayInfo (com.intellij.codeInsight.hints.InlayInfo)1 ResultItem (com.intellij.execution.filters.Filter.ResultItem)1 HyperlinkInfo (com.intellij.execution.filters.HyperlinkInfo)1 DocumentWindow (com.intellij.injected.editor.DocumentWindow)1 JSTestOptions (com.intellij.lang.javascript.JSTestOptions)1 Disposable (com.intellij.openapi.Disposable)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1 Result (com.intellij.openapi.application.Result)1