com.intellij.formatting.FormattingModelBuilder Java Examples
The following examples show how to use
com.intellij.formatting.FormattingModelBuilder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GraphQLInjectedFormattingModelBuilder.java From js-graphql-intellij-plugin with MIT License | 6 votes |
@NotNull @Override public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) { if(element instanceof JSFile || element.getContainingFile() instanceof JSFile) { final JSFile file = (JSFile)(element instanceof JSFile ? element : element.getContainingFile()); file.putUserData(WANT_DEFAULT_FORMATTER_KEY, true); try { final FormattingModelBuilder formattingModelBuilder = LanguageFormatting.INSTANCE.forContext(file.getLanguage(), element); if (formattingModelBuilder != null) { final FormattingModel model = formattingModelBuilder.createModel(element, settings); final Block rootBlock = model.getRootBlock(); return new DelegatingFormattingModel(model, new GraphQLBlockWrapper(rootBlock, null, element.getNode(), rootBlock.getWrap(), rootBlock.getAlignment(), createSpaceBuilder(settings, element), settings)); } } finally { file.putUserData(WANT_DEFAULT_FORMATTER_KEY, null); } } throw new IllegalArgumentException("Unsupported element '" + element + "'. It must be an element in a JSFile with its own default formatter to support injected GraphQL formatting"); }
Example #2
Source File: JoinLinesHandler.java From consulo with Apache License 2.0 | 6 votes |
private int[] getSpacesToAdd(List<RangeMarker> markers) { int size = markers.size(); int[] spacesToAdd = new int[size]; Arrays.fill(spacesToAdd, -1); CharSequence text = myDoc.getCharsSequence(); FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(myFile); CodeStyleSettings settings = CodeStyle.getSettings(myFile); FormattingModel model = builder == null ? null : builder.createModel(myFile, settings); FormatterEx formatter = FormatterEx.getInstance(); for (int i = 0; i < size; i++) { myIndicator.checkCanceled(); myIndicator.setFraction(0.7 + 0.25 * i / size); RangeMarker marker = markers.get(i); if (!marker.isValid()) continue; int end = StringUtil.skipWhitespaceForward(text, marker.getStartOffset()); int spacesToCreate = end >= text.length() || text.charAt(end) == '\n' ? 0 : model == null ? 1 : formatter.getSpacingForBlockAtOffset(model, end); spacesToAdd[i] = spacesToCreate < 0 ? 1 : spacesToCreate; } return spacesToAdd; }
Example #3
Source File: CodeStyleManagerRunnable.java From consulo with Apache License 2.0 | 6 votes |
private static TextRange getSignificantRange(final PsiFile file, final int offset) { final ASTNode elementAtOffset = SourceTreeToPsiMap.psiElementToTree(CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset)); if (elementAtOffset == null) { int significantRangeStart = CharArrayUtil.shiftBackward(file.getText(), offset - 1, "\n\r\t "); return new TextRange(Math.max(significantRangeStart, 0), offset); } final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file); if (builder != null) { final TextRange textRange = builder.getRangeAffectingIndent(file, offset, elementAtOffset); if (textRange != null) { return textRange; } } final TextRange elementRange = elementAtOffset.getTextRange(); if (isWhiteSpace(elementAtOffset)) { return extendRangeAtStartOffset(file, elementRange); } return elementRange; }
Example #4
Source File: LanguageFormatting.java From consulo with Apache License 2.0 | 5 votes |
@Nullable public FormattingModelBuilder forContext(@Nonnull Language language, @Nonnull PsiElement context) { for (LanguageFormattingRestriction each : LanguageFormattingRestriction.EXTENSION.getExtensionList()) { if (!each.isFormatterAllowed(context)) return null; } for (FormattingModelBuilder builder : allForLanguage(language)) { if (builder instanceof CustomFormattingModelBuilder) { final CustomFormattingModelBuilder custom = (CustomFormattingModelBuilder)builder; if (custom.isEngagedToFormat(context)) return builder; } } return forLanguage(language); }
Example #5
Source File: PsiViewerDialog.java From consulo with Apache License 2.0 | 5 votes |
@Nullable private static Block buildBlocks(@Nonnull PsiElement rootElement) { FormattingModelBuilder formattingModelBuilder = LanguageFormatting.INSTANCE.forContext(rootElement); CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(rootElement.getProject()); if (formattingModelBuilder != null) { FormattingModel formattingModel = formattingModelBuilder.createModel(rootElement, settings); return formattingModel.getRootBlock(); } else { return null; } }
Example #6
Source File: IndentOptionsDetectorImpl.java From consulo with Apache License 2.0 | 5 votes |
private List<LineIndentInfo> calcLineIndentInfo() { if (myDocument == null) return null; CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(myProject); FormattingModelBuilder modelBuilder = LanguageFormatting.INSTANCE.forContext(myFile); if (modelBuilder == null) return null; FormattingModel model = modelBuilder.createModel(myFile, settings); Block rootBlock = model.getRootBlock(); return new FormatterBasedLineIndentInfoBuilder(myDocument, rootBlock).build(); }
Example #7
Source File: LanguageFormatting.java From consulo with Apache License 2.0 | 4 votes |
@Nullable public FormattingModelBuilder forContext(@Nonnull PsiElement context) { return forContext(context.getLanguage(), context); }
Example #8
Source File: CodeStyleManagerRunnable.java From consulo with Apache License 2.0 | 4 votes |
public T perform(PsiFile file, int offset, @Nullable TextRange range, T defaultValue) { if (file instanceof PsiCompiledFile) { file = ((PsiCompiledFile)file).getDecompiledPsiFile(); } PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myCodeStyleManager.getProject()); Document document = documentManager.getDocument(file); if (document instanceof DocumentWindow) { final DocumentWindow documentWindow = (DocumentWindow)document; final PsiFile topLevelFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file); if (!file.equals(topLevelFile)) { if (range != null) { range = documentWindow.injectedToHost(range); } if (offset != -1) { offset = documentWindow.injectedToHost(offset); } return adjustResultForInjected(perform(topLevelFile, offset, range, defaultValue), documentWindow); } } final PsiFile templateFile = PsiUtilCore.getTemplateLanguageFile(file); if (templateFile != null) { file = templateFile; document = documentManager.getDocument(templateFile); } PsiElement element = null; if (offset != -1) { element = CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset); if (element == null && offset != file.getTextLength()) { return defaultValue; } if (isInsidePlainComment(offset, element)) { return computeValueInsidePlainComment(file, offset, defaultValue); } } final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file); FormattingModelBuilder elementBuilder = element != null ? LanguageFormatting.INSTANCE.forContext(element) : builder; if (builder != null && elementBuilder != null) { mySettings = CodeStyle.getSettings(file); mySignificantRange = offset != -1 ? getSignificantRange(file, offset) : null; myIndentOptions = mySettings.getIndentOptionsByFile(file, mySignificantRange); FormattingMode currentMode = myCodeStyleManager.getCurrentFormattingMode(); myCodeStyleManager.setCurrentFormattingMode(myMode); try { myModel = buildModel(builder, file, document); T result = doPerform(offset, range); if (result != null) { return result; } } finally { myCodeStyleManager.setCurrentFormattingMode(currentMode); } } return defaultValue; }
Example #9
Source File: CppLanguage.java From CppTools with Apache License 2.0 | 4 votes |
@Nullable public FormattingModelBuilder getFormattingModelBuilder() { if (true) return null; return new CppFormattingModelBuilder(); }