Search in sources :

Example 1 with SourceChange

use of org.dartlang.analysis.server.protocol.SourceChange in project intellij-plugins by JetBrains.

the class DartRenameDialog method fillTargetsAndUsageToEditIdMap.

private void fillTargetsAndUsageToEditIdMap(@NotNull final List<UsageTarget> usageTargets, @NotNull final Map<Usage, String> usageToEditIdMap) {
    final SourceChange change = myRefactoring.getChange();
    assert change != null;
    final DartAnalysisServerService service = DartAnalysisServerService.getInstance(myProject);
    final PsiManager psiManager = PsiManager.getInstance(myProject);
    for (SourceFileEdit fileEdit : change.getEdits()) {
        final VirtualFile file = AssistUtils.findVirtualFile(fileEdit);
        final PsiFile psiFile = file == null ? null : psiManager.findFile(file);
        if (psiFile == null)
            continue;
        for (SourceEdit sourceEdit : fileEdit.getEdits()) {
            final int offset = service.getConvertedOffset(file, sourceEdit.getOffset());
            final int length = service.getConvertedOffset(file, sourceEdit.getOffset() + sourceEdit.getLength()) - offset;
            final TextRange range = TextRange.create(offset, offset + length);
            final boolean potentialUsage = myRefactoring.getPotentialEdits().contains(sourceEdit.getId());
            final PsiElement usageElement = DartServerFindUsagesHandler.getUsagePsiElement(psiFile, range);
            if (usageElement != null) {
                if (DartComponentType.typeOf(usageElement) != null) {
                    usageTargets.add(new PsiElement2UsageTargetAdapter(usageElement));
                } else {
                    final UsageInfo usageInfo = DartServerFindUsagesHandler.getUsageInfo(usageElement, range, potentialUsage);
                    if (usageInfo != null) {
                        usageToEditIdMap.put(new UsageInfo2UsageAdapter(usageInfo), sourceEdit.getId());
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFileEdit(org.dartlang.analysis.server.protocol.SourceFileEdit) PsiManager(com.intellij.psi.PsiManager) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) TextRange(com.intellij.openapi.util.TextRange) SourceEdit(org.dartlang.analysis.server.protocol.SourceEdit) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) PsiFile(com.intellij.psi.PsiFile) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 2 with SourceChange

use of org.dartlang.analysis.server.protocol.SourceChange in project intellij-plugins by JetBrains.

the class DartServerStatementCompletionProcessor method process.

@Override
public boolean process(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile psiFile) {
    final int offset = editor.getCaretModel().getOffset();
    final DartAnalysisServerService service = DartAnalysisServerService.getInstance(psiFile.getProject());
    service.updateFilesContent();
    SourceChange sourceChange = service.edit_getStatementCompletion(psiFile.getVirtualFile(), offset);
    if (sourceChange != null && !isNoop(sourceChange)) {
        try {
            AssistUtils.applySourceChange(project, sourceChange, true);
            Position position = sourceChange.getSelection();
            if (position != null) {
                // The position should never be null but it might be if unit tests are flaky.
                editor.getCaretModel().moveToOffset(service.getConvertedOffset(psiFile.getVirtualFile(), position.getOffset()));
            }
        } catch (DartSourceEditException e) {
            CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), CommonBundle.getErrorTitle(), null);
        }
        return true;
    }
    return false;
}
Also used : Position(org.dartlang.analysis.server.protocol.Position) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Example 3 with SourceChange

use of org.dartlang.analysis.server.protocol.SourceChange in project intellij-plugins by JetBrains.

the class DartExtractLocalVariableRefactoringTest method doTest.

private void doTest(String filePath, boolean all) {
    final ServerExtractLocalVariableRefactoring refactoring = createRefactoring(filePath);
    // check initial conditions
    final RefactoringStatus initialConditions = refactoring.checkInitialConditions();
    assertNotNull(initialConditions);
    assertTrue(initialConditions.isOK());
    // configure
    //refactoring.setName("test");
    refactoring.setExtractAll(all);
    // check final conditions
    final RefactoringStatus finalConditions = refactoring.checkFinalConditions();
    assertNotNull(finalConditions);
    assertTrue(finalConditions.isOK());
    // apply the SourceChange
    final SourceChange change = refactoring.getChange();
    assertNotNull(change);
    ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            AssistUtils.applySourceChange(myFixture.getProject(), change, true);
        } catch (DartSourceEditException e) {
            fail(e.getMessage());
        }
    });
    // validate
    myFixture.checkResultByFile(getTestName(false) + ".after.dart");
}
Also used : ServerExtractLocalVariableRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerExtractLocalVariableRefactoring) RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Example 4 with SourceChange

use of org.dartlang.analysis.server.protocol.SourceChange in project intellij-plugins by JetBrains.

the class DartExtractMethodRefactoringTest method doTest.

private void doTest(String filePath, boolean all, boolean asGetter) {
    final ServerExtractMethodRefactoring refactoring = createRefactoring(filePath);
    // check initial conditions
    final RefactoringStatus initialConditions = refactoring.checkInitialConditions();
    assertNotNull(initialConditions);
    assertTrue(initialConditions.isOK());
    // configure
    refactoring.setName("test");
    refactoring.setExtractAll(all);
    refactoring.setCreateGetter(asGetter);
    // check final conditions
    final RefactoringStatus finalConditions = refactoring.checkFinalConditions();
    assertNotNull(finalConditions);
    assertTrue(finalConditions.isOK());
    // apply the SourceChange
    final SourceChange change = refactoring.getChange();
    assertNotNull(change);
    ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            AssistUtils.applySourceChange(myFixture.getProject(), change, false);
        } catch (DartSourceEditException e) {
            fail(e.getMessage());
        }
    });
    // validate
    myFixture.checkResultByFile(getTestName(false) + ".after.dart");
}
Also used : ServerExtractMethodRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerExtractMethodRefactoring) RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Example 5 with SourceChange

use of org.dartlang.analysis.server.protocol.SourceChange in project intellij-plugins by JetBrains.

the class DartInlineMethodRefactoringTest method doTest.

private void doTest(String filePath, boolean all) {
    final ServerInlineMethodRefactoring refactoring = createRefactoring(filePath);
    // check initial conditions
    final RefactoringStatus initialConditions = refactoring.checkInitialConditions();
    assertNotNull(initialConditions);
    assertTrue(initialConditions.isOK());
    // all
    if (all) {
        refactoring.setInlineAll(true);
        refactoring.setDeleteSource(true);
    }
    // check final conditions
    final RefactoringStatus finalConditions = refactoring.checkFinalConditions();
    assertNotNull(finalConditions);
    assertTrue(finalConditions.isOK());
    // apply the SourceChange
    final SourceChange change = refactoring.getChange();
    assertNotNull(change);
    ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            AssistUtils.applySourceChange(myFixture.getProject(), change, false);
        } catch (DartSourceEditException e) {
            fail(e.getMessage());
        }
    });
    // validate
    myFixture.checkResultByFile(getTestName(false) + ".after.dart");
}
Also used : RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) ServerInlineMethodRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerInlineMethodRefactoring) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Aggregations

SourceChange (org.dartlang.analysis.server.protocol.SourceChange)12 DartSourceEditException (com.jetbrains.lang.dart.assists.DartSourceEditException)8 RefactoringStatus (com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus)7 DartAnalysisServerService (com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)2 JsonObject (com.google.gson.JsonObject)1 PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiManager (com.intellij.psi.PsiManager)1 UsageInfo (com.intellij.usageView.UsageInfo)1 SmartList (com.intellij.util.SmartList)1 ServerExtractLocalVariableRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerExtractLocalVariableRefactoring)1 ServerExtractMethodRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerExtractMethodRefactoring)1 ServerInlineLocalRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerInlineLocalRefactoring)1 ServerInlineMethodRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerInlineMethodRefactoring)1 THashMap (gnu.trove.THashMap)1 Position (org.dartlang.analysis.server.protocol.Position)1 RefactoringFeedback (org.dartlang.analysis.server.protocol.RefactoringFeedback)1