@Override public PsiElement reformat(PsiElement element, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException { return delegate.reformat(element, canChangeWhiteSpacesOnly); }
@Override public void reformatText(PsiFile file, Collection<TextRange> ranges) throws IncorrectOperationException { delegate.reformatText(file, ranges); }
@Override public void adjustLineIndent(PsiFile file, TextRange rangeToAdjust) throws IncorrectOperationException { delegate.adjustLineIndent(file, rangeToAdjust); }
private String doTest(@Nullable Character c, String testName) { if (c == null) { WriteCommandAction.runWriteCommandAction(myFixture.getProject(), () -> { CodeStyleManager.getInstance(getProject()).reformat(myFixture.getFile()); }); } else { myFixture.type(c); } return String.format("%s-after.go", testName); } }
@Override protected void run() throws Throwable { CodeStyleManager.getInstance(getProject()).reformatText(myFixture.getFile(), ContainerUtil.newArrayList(myFixture.getFile().getTextRange())); } }.execute();
@Override public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext) { if (myOriginalHandler != null) myOriginalHandler.execute(editor, c, dataContext); if (c != 'e') return; Project project = editor.getProject(); if (project == null) return; int offset = editor.getCaretModel().getOffset(); if (offset < 4) return; TextRange from = TextRange.from(offset - 4, 4); String text = editor.getDocument().getText(from); if ("case".equals(text)) { PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); ApplicationManager.getApplication().runWriteAction(() -> { if (project.isDisposed()) return; PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project); if (file == null) return; CodeStyleManager.getInstance(project).adjustLineIndent(file, from); }); } } }
private static void installFormatter(Project project) { CodeStyleManager currentManager = CodeStyleManager.getInstance(project); if (currentManager instanceof GoogleJavaFormatCodeStyleManager) { currentManager = ((GoogleJavaFormatCodeStyleManager) currentManager).getDelegate(); } setManager(project, new GoogleJavaFormatCodeStyleManager(currentManager)); }
private static boolean isCastRedundantInRefExpression (final PsiReferenceExpression refExpression, final PsiExpression castOperand) { final PsiElement resolved = refExpression.resolve(); final Ref<Boolean> result = new Ref<Boolean>(Boolean.FALSE); CodeStyleManager.getInstance(refExpression.getProject()).performActionWithFormatterDisabled(new Runnable() { @Override public void run() { try { final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(refExpression.getProject()).getElementFactory(); final PsiExpression copyExpression = elementFactory.createExpressionFromText(refExpression.getText(), refExpression); if (copyExpression instanceof PsiReferenceExpression) { final PsiReferenceExpression copy = (PsiReferenceExpression)copyExpression; final PsiExpression qualifier = copy.getQualifierExpression(); if (qualifier != null) { qualifier.replace(castOperand); result.set(Boolean.valueOf(copy.resolve() == resolved)); } } } catch (IncorrectOperationException ignore) { } } }); return result.get().booleanValue(); }
CodeStyleManager.getInstance(file.getProject()).reformatRange(file, addOffset, addOffset + closeMarker.length());
@Override public <T extends Throwable> void performActionWithFormatterDisabled(ThrowableRunnable<T> r) throws T { delegate.performActionWithFormatterDisabled(r); }
private void doTestBBEFile(File resource, @Nullable Character c) { String relativeFilePath = resource.getPath().replace(getTestDataPath(), EMPTY_STRING); myFixture.configureByFile(relativeFilePath); if (c == null) { WriteCommandAction.runWriteCommandAction(myFixture.getProject(), () -> { CodeStyleManager.getInstance(getProject()).reformat(myFixture.getFile()); }); } else { myFixture.type(c); } myFixture.checkResultByFile(relativeFilePath, relativeFilePath.replace("examples", "expectedResults"), true); // To debug - Comment the above line and uncomment these lines and add expected result. // String result = ""; // myFixture.checkResult(result); } }
@Override protected void run(@NotNull Result result) throws Throwable { StringBuffer textBuf = new StringBuffer(); textBuf.append("\n"); textBuf.append(methodCreated.getText()); editor.getDocument().insertString(insertPos, textBuf); final int endPos = insertPos + textBuf.length(); CodeStyleManager.getInstance(project).reformatText(phpClass.getContainingFile(), insertPos, endPos); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); Method insertedMethod = phpClass.findMethodByName(functionName); if(insertedMethod != null) { editor.getCaretModel().moveToOffset(insertedMethod.getTextRange().getStartOffset()); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); } }
CodeStyleManager.getInstance(context.getProject()).adjustLineIndent(document, offsetToMove);
@Override public void performActionWithFormatterDisabled(Runnable r) { delegate.performActionWithFormatterDisabled(r); }
@NotNull public static PsiElement createBundleFile(@NotNull PhpClass bundleClass, @NotNull String template, @NotNull String className, Map<String, String> vars) throws Exception { VirtualFile directory = bundleClass.getContainingFile().getContainingDirectory().getVirtualFile(); if(fileExists(directory, new String[] {className})) { throw new Exception("File already exists"); } String COMPILER_TEMPLATE = "/fileTemplates/" + template + ".php"; String fileTemplateContent = getFileTemplateContent(COMPILER_TEMPLATE); if(fileTemplateContent == null) { throw new Exception("Template content error"); } String[] split = className.split("\\\\"); String ns = bundleClass.getNamespaceName(); String join = StringUtils.join(Arrays.copyOf(split, split.length - 1), "/"); vars.put("ns", (ns.startsWith("\\") ? ns.substring(1) : ns) + join.replace("/", "\\")); vars.put("class", split[split.length - 1]); for (Map.Entry<String, String> entry : vars.entrySet()) { fileTemplateContent = fileTemplateContent.replace("{{ " + entry.getKey() + " }}", entry.getValue()); } VirtualFile compilerDirectory = getAndCreateDirectory(directory, join); if(compilerDirectory == null) { throw new Exception("Directory creation failed"); } Project project = bundleClass.getProject(); PsiFile fileFromText = PsiFileFactory.getInstance(project).createFileFromText(split[split.length - 1] + ".php", PhpFileType.INSTANCE, fileTemplateContent); CodeStyleManager.getInstance(project).reformat(fileFromText); return PsiDirectoryFactory.getInstance(project).createDirectory(compilerDirectory).add(fileFromText); }
@Override protected PsiFile doReformat(Project project, PsiFile psiFile) { CodeStyleManager.getInstance(project).reformatText(psiFile, 0, psiFile.getTextLength()); return psiFile; } }
private void doIndent(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext, EditorActionHandler originalHandler, Document document) { originalHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), dataContext); PsiDocumentManager.getInstance(file.getProject()).commitDocument(document); try { CodeStyleManager.getInstance(file.getProject()).adjustLineIndent(file, editor.getCaretModel().getOffset()); } catch (IncorrectOperationException e) { LOG.error(e); } } }
@Override public PsiElement reformat(PsiElement element) throws IncorrectOperationException { return delegate.reformat(element); }
@Override public void reformatText(PsiFile file, int startOffset, int endOffset) throws IncorrectOperationException { delegate.reformatText(file, startOffset, endOffset); }
@Override public int adjustLineIndent(Document document, int offset) { return delegate.adjustLineIndent(document, offset); }