Search in sources :

Example 66 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project buck by facebook.

the class BuckModule method projectOpened.

@Override
public void projectOpened() {
    projectClosed = new AtomicBoolean(false);
    BuckFileUtil.setBuckFileType();
    // connect to the Buck client
    BuckClientManager.getOrCreateClient(mProject, mEventHandler).connect();
    if (!UISettings.getInstance().SHOW_MAIN_TOOLBAR) {
        BuckPluginNotifications.notifyActionToolbar(mProject);
    }
    mBuckEventsConsumer = new BuckEventsConsumer(mProject);
    PsiDocumentManager manager = PsiDocumentManager.getInstance(mProject);
    manager.addListener(new BuckAutoDepsContributor(mProject));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BuckEventsConsumer(com.facebook.buck.intellij.ideabuck.ui.BuckEventsConsumer) BuckAutoDepsContributor(com.facebook.buck.intellij.ideabuck.autodeps.BuckAutoDepsContributor) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 67 with PsiDocumentManager

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

the class GradleFileEditor method refresh.

public void refresh() {
    if (myDocument != null) {
        PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myProject);
        psiDocumentManager.commitDocument(myDocument);
    }
    disposeGroups();
    myGroups = new GradleEditorModelParserFacade().parse(myVirtualFile, myProject);
    myComponent.setData(myGroups);
}
Also used : GradleEditorModelParserFacade(com.android.tools.idea.gradle.editor.parser.GradleEditorModelParserFacade) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 68 with PsiDocumentManager

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

the class AndroidModuleInfoTest method testManifestPlaceholderCompletion.

public void testManifestPlaceholderCompletion() throws Exception {
    loadProject(MODULE_INFO_MERGE);
    assertNotNull(myAndroidFacet);
    VirtualFile file = getProject().getBaseDir().findFileByRelativePath("src/main/AndroidManifest.xml");
    assertNotNull(file);
    PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(file);
    assertNotNull(psiFile);
    myFixture.configureFromExistingVirtualFile(file);
    List<HighlightInfo> highlights = myFixture.doHighlighting(HighlightSeverity.ERROR);
    assertTrue(highlights.isEmpty());
    myFixture.testHighlighting(false, false, false, file);
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
    final Document document = manager.getDocument(psiFile);
    assertNotNull(document);
    final String defaultPlaceholder = "${defaultTheme}";
    // Check placeholder completion
    new WriteCommandAction.Simple(getProject(), psiFile) {

        @Override
        protected void run() throws Throwable {
            int offset = document.getText().indexOf(defaultPlaceholder);
            document.replaceString(offset, offset + defaultPlaceholder.length(), "${<caret>");
            manager.commitAllDocuments();
        }
    }.execute();
    FileDocumentManager.getInstance().saveAllDocuments();
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.complete(CompletionType.BASIC);
    assertThat(myFixture.getLookupElementStrings()).containsExactly("defaultTheme");
    // Check that there are no errors
    myFixture.testHighlighting();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 69 with PsiDocumentManager

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

the class LombokPsiConverterTest method check.

private static void check(PsiFile psiFile, @Language("JAVA") String source) {
    assertTrue(psiFile.getClass().getName(), psiFile instanceof PsiJavaFile);
    PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile;
    CompilationUnit node = LombokPsiConverter.convert(psiJavaFile);
    assertNotNull(node);
    String actualStructure;
    if (CHECK_POSITIONS) {
        StructureFormatter structureFormatter = StructureFormatter.formatterWithPositions();
        node.accept(new SourcePrinter(structureFormatter));
        actualStructure = structureFormatter.finish();
    }
    TextFormatter formatter = new TextFormatter();
    node.accept(new SourcePrinter(formatter));
    String actual = formatter.finish();
    Node expectedNode = parse(source);
    assertNotNull(expectedNode);
    if (CHECK_POSITIONS) {
        StructureFormatter structureFormatter = StructureFormatter.formatterWithPositions();
        expectedNode.accept(new SourcePrinter(structureFormatter));
        String masterStructure = structureFormatter.finish();
        assertEquals(masterStructure, actualStructure);
    }
    formatter = new TextFormatter();
    expectedNode.accept(new SourcePrinter(formatter));
    String master = formatter.finish();
    assertEquals(master, actual);
    // Check for resilience to error nodes being present in the AST
    Project project = psiFile.getProject();
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
    final Document document = manager.getDocument(psiFile);
    assertNotNull(document);
    // fixed seed for test reproducibility
    final Random random = new Random(0L);
    for (int i = 0; i < 500; i++) {
        WriteCommandAction.runWriteCommandAction(project, new Runnable() {

            @Override
            public void run() {
                int pos = random.nextInt(document.getTextLength() - 1);
                char ch = (char) (random.nextInt(64) + 32);
                double operation = random.nextDouble();
                if (operation < 0.33) {
                    document.insertString(pos, Character.toString(ch));
                } else if (operation < 0.67) {
                    document.replaceString(pos, pos + 1, Character.toString(ch));
                } else {
                    document.deleteString(pos, pos + 1);
                }
                manager.commitDocument(document);
            }
        });
        node = LombokPsiConverter.convert(psiJavaFile);
        assertNotNull(psiJavaFile.getText(), node);
    }
}
Also used : CompilationUnit(lombok.ast.CompilationUnit) SourcePrinter(lombok.ast.printer.SourcePrinter) TextFormatter(lombok.ast.printer.TextFormatter) StructureFormatter(lombok.ast.printer.StructureFormatter) Node(lombok.ast.Node) PsiJavaFile(com.intellij.psi.PsiJavaFile) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) Random(java.util.Random) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 70 with PsiDocumentManager

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

the class ResourceFolderRepositoryTest method testRemoveStringArrayItemElements.

public void testRemoveStringArrayItemElements() 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, "security_questions"));
    ResourceItem array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
    ResourceValue resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("Question 4", resourceValue.getValue());
    assertTrue(resourceValue instanceof ArrayResourceValue);
    ArrayResourceValue arv = (ArrayResourceValue) resourceValue;
    assertEquals(5, arv.getElementCount());
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            String elementString = "<item>Question 3</item>";
            final int offset = document.getText().indexOf(elementString);
            document.deleteString(offset, offset + elementString.length());
            document.insertString(offset, "<item>Question X</item>");
            documentManager.commitDocument(document);
        }
    });
    // First edit won't be incremental (file -> Psi).
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
    array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
    resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("Question 4", resourceValue.getValue());
    assertTrue(resourceValue instanceof ArrayResourceValue);
    arv = (ArrayResourceValue) resourceValue;
    assertEquals(5, arv.getElementCount());
    resetScanCounter();
    // Now try another edit that is also a delete item, where things should be incremental now.
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            String elementString = "<item>Question X</item>";
            final int offset = document.getText().indexOf(elementString);
            document.deleteString(offset, offset + elementString.length());
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
    array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
    resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("Question 5", resourceValue.getValue());
    assertTrue(resourceValue instanceof ArrayResourceValue);
    arv = (ArrayResourceValue) resourceValue;
    assertEquals(4, arv.getElementCount());
    // 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)

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