@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (editor == null) { LOG.error("Cannot run quick fix without editor: " + getClass().getSimpleName(), AttachmentFactory.createAttachment(file.getVirtualFile())); return; } if (!(startElement instanceof GoType)) return; GoType type = (GoType)startElement; PsiElement anchor = PsiTreeUtil.findPrevParent(file, type); String name = "TypeName"; GoTypeDeclaration decl = (GoTypeDeclaration)file.addBefore(GoElementFactory.createTypeDeclaration(project, name, type), anchor); if (decl == null) return; decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl); if (decl == null) return; GoTypeSpec spec = ContainerUtil.getFirstItem(decl.getTypeSpecList()); if (spec == null) return; TemplateBuilderImpl builder = new TemplateBuilderImpl(file); builder.replaceElement(type, OTHER_NAME, INPUT_NAME, false); builder.replaceElement(spec.getIdentifier(), INPUT_NAME, new ConstantNode(name), true); editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset()); Template template = builder.buildInlineTemplate(); editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset()); TemplateManager.getInstance(project).startTemplate(editor, template); }
@Override protected void run() throws Throwable { CodeStyleManager.getInstance(getProject()).reformatText(myFixture.getFile(), ContainerUtil.newArrayList(myFixture.getFile().getTextRange())); } }.execute();
@Before public void set() { super.register(app, SonarLintGlobalSettings.class, globalSettings); super.register(SonarLintProjectSettings.class, projectSettings); when(psiFile.getTextRange()).thenReturn(psiFileRange); when(psiFile.getVirtualFile()).thenReturn(virtualFile); when(psiFile.getFileType()).thenReturn(JavaFileType.INSTANCE); }
@Override protected void run() throws Throwable { CodeStyleManager.getInstance(getProject()).reformatText(myFixture.getFile(), ContainerUtil.newArrayList(myFixture.getFile().getTextRange())); } }.execute();
@Override protected void run() throws Throwable { CodeStyleManager.getInstance(getProject()).reformatText(myFixture.getFile(), ContainerUtil.newArrayList(myFixture.getFile().getTextRange())); } }.execute();
protected void doTest(String actual, String expected) { PsiFile file = myFixture.addFileToProject("test.cypher", actual); myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); WriteCommandAction.runWriteCommandAction(getProject(), () -> { PsiFile f = myFixture.getFile(); CodeStyleManager.getInstance(getProject()) .reformatText(f, singletonList(f.getTextRange())); }); myFixture.checkResult(expected); } }
protected void doFormatTestWithoutInitialization(@NotNull String resultFileName, @NotNull String resultSuffix) { WriteCommandAction.writeCommandAction(getProject()).run(() -> { PsiFile file = myFixture.getFile(); if (file.getViewProvider() instanceof InjectedFileViewProvider) { //noinspection ConstantConditions file = file.getContext().getContainingFile(); } TextRange rangeToUse = file.getTextRange(); CodeStyleManager.getInstance(getProject()).reformatText(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset()); }); String resultFilePath = getTestDataPath() + "/" + resultFileName + resultSuffix + ".txt"; UsefulTestCase.assertSameLinesWithFile(resultFilePath, myFixture.getFile().getText()); assertNoErrorElements(); }
private void addAnnotation(LiveIssue issue, AnnotationHolder annotationHolder) { TextRange textRange; if (issue.getRange() != null) { textRange = createTextRange(issue.getRange()); } else { textRange = issue.psiFile().getTextRange(); } String htmlMsg = getHtmlMessage(issue); Annotation annotation = annotationHolder .createAnnotation(getSeverity(issue.getSeverity()), textRange, issue.getMessage(), htmlMsg); annotation.registerFix(new DisableRuleQuickFix(issue.getRuleKey())); if (!issue.flows().isEmpty()) { annotation.registerFix(new ShowLocationsIntention(issue.getRange(), issue.getMessage(), issue.flows())); } if (issue.getRange() == null) { annotation.setFileLevelAnnotation(true); } else { annotation.setTextAttributes(getTextAttrsKey(issue.getSeverity())); } /* * 3 possible ways to set text attributes and error stripe color: * - enforce text attributes ({@link Annotation#setEnforcedTextAttributes}) and we need to set everything * manually (including error stripe color). This won't be configurable in a standard way and won't change based on used color scheme * - rely on one of the default attributes by giving a key {@link com.intellij.openapi.editor.colors.CodeInsightColors} or your own * key (SonarLintTextAttributes) to Annotation#setTextAttributes * - let Annotation#getTextAttributes decide it based on highlight type and severity. */ annotation.setHighlightType(getType(issue.getSeverity())); }