Search in sources :

Example 56 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.

the class ResourceFolderRepositoryTest method testEditStyleItemText.

public void testEditStyleItemText() throws Exception {
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkTheme"));
    ResourceItem style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
    StyleResourceValue srv = (StyleResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    ResourceValue actionBarStyle = srv.getItem("actionBarStyle", true);
    assertNotNull(actionBarStyle);
    assertEquals("@style/DarkActionBar", actionBarStyle.getValue());
    long generation = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("@style/DarkActionBar");
            document.replaceString(offset + 7, offset + 11, "Grey");
            documentManager.commitDocument(document);
        }
    });
    // First edit won't be incremental (file -> Psi).
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(generation < resources.getModificationCount());
    assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkTheme"));
    style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
    srv = (StyleResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    actionBarStyle = srv.getItem("actionBarStyle", true);
    assertNotNull(actionBarStyle);
    assertEquals("@style/GreyActionBar", actionBarStyle.getValue());
    resetScanCounter();
    // Now try another edit where things should be incremental now.
    long generation2 = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("@style/GreyActionBar");
            document.replaceString(offset + 7, offset + 11, "Light");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(generation2 < resources.getModificationCount());
    assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkTheme"));
    style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
    srv = (StyleResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    actionBarStyle = srv.getItem("actionBarStyle", true);
    assertNotNull(actionBarStyle);
    assertEquals("@style/LightActionBar", actionBarStyle.getValue());
    // Shouldn't have done any full file rescans during the above edits
    ensureIncremental();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) ResourceItem(com.android.ide.common.res2.ResourceItem) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 57 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.

the class ResourceFolderRepositoryTest method testEditAttr.

public void testEditAttr() throws Exception {
    // Insert, remove and change <attr> attributes inside a <declare-styleable> and ensure that
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    final ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
    // Fetch resource value to ensure it gets replaced after update
    assertTrue(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
    ResourceItem style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
    DeclareStyleableResourceValue srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    assertEquals(5, srv.getAllAttributes().size());
    AttrResourceValue watchType = findAttr(srv.getAllAttributes(), "watchType");
    assertNotNull(watchType);
    assertEquals(2, watchType.getAttributeValues().size());
    assertEquals(Integer.valueOf(1), watchType.getAttributeValues().get("type_stopwatch"));
    assertEquals(Integer.valueOf(0), watchType.getAttributeValues().get("type_countdown"));
    AttrResourceValue crash = findAttr(srv.getAllAttributes(), "crash");
    assertNotNull(crash);
    assertNull(crash.getAttributeValues());
    final long generation = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("watchType");
            document.insertString(offset, "y");
            documentManager.commitDocument(document);
        }
    });
    // First edit won't be incremental (file -> Psi).
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(generation < resources.getModificationCount());
    assertFalse(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
    assertTrue(resources.hasResourceItem(ResourceType.ATTR, "ywatchType"));
    resetScanCounter();
    // However, the second edit can then be incremental.
    final long generation2 = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("ywatchType");
            document.replaceString(offset, offset + 1, "w");
            documentManager.commitDocument(document);
        }
    });
    assertFalse(resources.isScanPending(psiFile1));
    assertTrue(generation2 < resources.getModificationCount());
    assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
    assertFalse(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
    assertTrue(resources.hasResourceItem(ResourceType.ATTR, "wwatchType"));
    style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
    srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    assertEquals(5, srv.getAllAttributes().size());
    watchType = findAttr(srv.getAllAttributes(), "wwatchType");
    assertNotNull(watchType);
    assertEquals(2, watchType.getAttributeValues().size());
    assertEquals(Integer.valueOf(1), watchType.getAttributeValues().get("type_stopwatch"));
    // Shouldn't have done any full file rescans during the above edits
    ensureIncremental();
    // Now insert a new item and delete one and make sure we're still okay
    resetScanCounter();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            String crashAttr = "<attr name=\"crash\" format=\"boolean\" />";
            int offset = document.getText().indexOf(crashAttr);
            document.deleteString(offset, offset + crashAttr.length());
            document.insertString(offset, "<attr name=\"newcrash\" format=\"integer\" />");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

        @Override
        public void run() {
            ensureSingleScan();
            assertTrue(generation2 < resources.getModificationCount());
            assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
            assertFalse(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
            assertTrue(resources.hasResourceItem(ResourceType.ATTR, "wwatchType"));
            ResourceItem style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
            DeclareStyleableResourceValue srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
            assertNotNull(srv);
            assertEquals(5, srv.getAllAttributes().size());
            AttrResourceValue watchType = findAttr(srv.getAllAttributes(), "wwatchType");
            assertNotNull(watchType);
            assertEquals(2, watchType.getAttributeValues().size());
            assertEquals(Integer.valueOf(1), watchType.getAttributeValues().get("type_stopwatch"));
            assertEquals(Integer.valueOf(0), watchType.getAttributeValues().get("type_countdown"));
            AttrResourceValue crash = findAttr(srv.getAllAttributes(), "crash");
            assertNull(crash);
            AttrResourceValue newcrash = findAttr(srv.getAllAttributes(), "newcrash");
            assertNotNull(newcrash);
            assertNull(newcrash.getAttributeValues());
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) ResourceItem(com.android.ide.common.res2.ResourceItem) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 58 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.

the class ResourceFolderRepositoryTest method testAddIntegerArrayItemElements.

public void testAddIntegerArrayItemElements() throws Exception {
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
    ResourceItem array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
    ResourceValue resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("10", resourceValue.getValue());
    assertTrue(resourceValue instanceof ArrayResourceValue);
    ArrayResourceValue arv = (ArrayResourceValue) resourceValue;
    assertEquals(2, arv.getElementCount());
    assertEquals("10", arv.getElement(0));
    assertEquals("20", arv.getElement(1));
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("<item>10</item>");
            document.insertString(offset, "<item>5</item>");
            documentManager.commitDocument(document);
        }
    });
    // First edit won't be incremental (file -> Psi).
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
    array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
    resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("5", resourceValue.getValue());
    assertTrue(resourceValue instanceof ArrayResourceValue);
    arv = (ArrayResourceValue) resourceValue;
    assertEquals(3, arv.getElementCount());
    assertEquals("5", arv.getElement(0));
    assertEquals("10", arv.getElement(1));
    assertEquals("20", arv.getElement(2));
    resetScanCounter();
    // Now try a second edit.
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("<item>5</item>");
            document.insertString(offset, "<item>2</item>");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
    array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
    resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("2", resourceValue.getValue());
    assertTrue(resourceValue instanceof ArrayResourceValue);
    arv = (ArrayResourceValue) resourceValue;
    assertEquals(4, arv.getElementCount());
    assertEquals("2", arv.getElement(0));
    assertEquals("5", arv.getElement(1));
    assertEquals("10", arv.getElement(2));
    assertEquals("20", arv.getElement(3));
    // Shouldn't have done any full file rescans during the above edits
    ensureIncremental();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) ResourceItem(com.android.ide.common.res2.ResourceItem) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 59 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project ballerina by ballerina-lang.

the class BallerinaRecursiveCallMarkerProvider method collectSlowLineMarkers.

@Override
public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) {
    // This is used to prevent adding multiple line markers to the same line.
    Set<Integer> lines = ContainerUtil.newHashSet();
    for (PsiElement element : elements) {
        // Check whether the current element can be used to add recursive call marker.
        if (!isMatchingElement(element)) {
            continue;
        }
        // Resolve the element to the definition. We use this to get the common context later.
        PsiElement resolvedElement = resolveElement(element);
        if (resolvedElement == null) {
            continue;
        }
        if (!(resolvedElement.getParent() instanceof FunctionDefinitionNode)) {
            continue;
        }
        // Get the document manager;
        PsiDocumentManager documentManager = PsiDocumentManager.getInstance(element.getProject());
        // Get the document.
        Document document = documentManager.getDocument(element.getContainingFile());
        if (document == null) {
            continue;
        }
        // Get the offset of the current element.
        int textOffset = element.getTextOffset();
        // Get the line number of the current element.
        int lineNumber = document.getLineNumber(textOffset);
        // Find the common context. For a recursive call, the common context should be a FunctionDefinitionNode.
        PsiElement commonContext = PsiTreeUtil.findCommonContext(element, resolvedElement);
        if (commonContext instanceof FunctionDefinitionNode && !lines.contains(lineNumber)) {
            // Add the number to the set.
            lines.add(lineNumber);
            // Return a new line marker.
            result.add(new RecursiveMethodCallMarkerInfo(element));
        }
    }
}
Also used : FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) Document(com.intellij.openapi.editor.Document) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 60 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project flutter-intellij by flutter.

the class FlutterTestLineMarkerContributor method getTestStateIcon.

@NotNull
private static Icon getTestStateIcon(@NotNull PsiElement element, @NotNull Icon defaultIcon) {
    // SMTTestProxy maps test run data to a URI derived from a location hint produced by `package:test`.
    // If we can find corresponding data, we can provide state-aware icons. If not, we default to
    // a standard Run state.
    PsiFile containingFile;
    try {
        containingFile = element.getContainingFile();
    } catch (PsiInvalidElementAccessException e) {
        containingFile = null;
    }
    final Project project = element.getProject();
    final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
    final Document document = containingFile == null ? null : psiDocumentManager.getDocument(containingFile);
    if (document != null) {
        final int textOffset = element.getTextOffset();
        final int lineNumber = document.getLineNumber(textOffset);
        // e.g., dart_location:///Users/pq/IdeaProjects/untitled1298891289891/test/unit_test.dart,3,2,["my first unit test"]
        final String path = containingFile.getVirtualFile().getPath();
        final String testLocationPrefix = "dart_location://" + path + "," + lineNumber;
        final TestStateStorage storage = TestStateStorage.getInstance(project);
        if (storage != null) {
            final Map<String, TestStateStorage.Record> tests = storage.getRecentTests(SCANNED_TEST_RESULT_LIMIT, getSinceDate());
            if (tests != null) {
                // TODO(pq): investigate performance implications.
                for (Map.Entry<String, TestStateStorage.Record> entry : tests.entrySet()) {
                    if (entry.getKey().startsWith(testLocationPrefix)) {
                        final TestStateStorage.Record state = entry.getValue();
                        final TestStateInfo.Magnitude magnitude = TestIconMapper.getMagnitude(state.magnitude);
                        if (magnitude != null) {
                            switch(magnitude) {
                                case IGNORED_INDEX:
                                    return AllIcons.RunConfigurations.TestState.Yellow2;
                                case ERROR_INDEX:
                                case FAILED_INDEX:
                                    return AllIcons.RunConfigurations.TestState.Red2;
                                case PASSED_INDEX:
                                case COMPLETE_INDEX:
                                    return AllIcons.RunConfigurations.TestState.Green2;
                                default:
                            }
                        }
                    }
                }
            }
        }
    }
    return defaultIcon;
}
Also used : PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) TestStateStorage(com.intellij.execution.TestStateStorage) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Map(java.util.Map) TestStateInfo(com.intellij.execution.testframework.sm.runner.states.TestStateInfo) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiDocumentManager (com.intellij.psi.PsiDocumentManager)140 Document (com.intellij.openapi.editor.Document)111 PsiFile (com.intellij.psi.PsiFile)100 VirtualFile (com.intellij.openapi.vfs.VirtualFile)51 ResourceItem (com.android.ide.common.res2.ResourceItem)26 PsiElement (com.intellij.psi.PsiElement)24 Project (com.intellij.openapi.project.Project)22 TextRange (com.intellij.openapi.util.TextRange)13 NotNull (org.jetbrains.annotations.NotNull)12 Nullable (org.jetbrains.annotations.Nullable)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 Editor (com.intellij.openapi.editor.Editor)7 XmlFile (com.intellij.psi.xml.XmlFile)6 FileType (com.intellij.openapi.fileTypes.FileType)5 XmlTag (com.intellij.psi.xml.XmlTag)5 ASTNode (com.intellij.lang.ASTNode)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)2 EditorEx (com.intellij.openapi.editor.ex.EditorEx)2