public static TextEdit reformat(int kind, String string, int indentationLevel, String lineSeparator, Map options) { return reformat(kind, string, 0, string.length(), indentationLevel, lineSeparator, options); }
/** * Creates edits that describe how to re-format the given string. * This method should be used for formatting existing code. * Returns <code>null</code> if the code could not be formatted for the given kind. * * @param kind * Use to specify the kind of the code snippet to format. * It can be any of the kind constants defined in {@link CodeFormatter} * @param source * The source to format * @param indentationLevel * The initial indentation level, used to shift left/right the entire source fragment. * An initial indentation level of zero or below has no effect. * @param lineSeparator * The line separator to use in formatted source, * if set to <code>null</code>, then the platform default one will be used. * @param options * The options map to use for formatting with the default code formatter. * Recognized options are documented on {@link JavaCore#getDefaultOptions()}. * If set to <code>null</code>, then use the current settings from {@link JavaCore#getOptions()}. * @return an TextEdit describing the changes required to format source * @throws IllegalArgumentException * If the offset and length are not inside the string, a IllegalArgumentException is thrown. */ public static TextEdit reformat(int kind, String source, int indentationLevel, String lineSeparator, Map<String, String> options) { return reformat(kind, source, 0, source.length(), indentationLevel, lineSeparator, options); }
/** * Creates edits that describe how to re-format the given string. * This method should be used for formatting existing code. * Returns <code>null</code> if the code could not be formatted for the given kind. * * @param kind * Use to specify the kind of the code snippet to format. * It can be any of the kind constants defined in {@link CodeFormatter} * @param source * The source to format * @param indentationLevel * The initial indentation level, used to shift left/right the entire source fragment. * An initial indentation level of zero or below has no effect. * @param lineSeparator * The line separator to use in formatted source, * if set to <code>null</code>, then the platform default one will be used. * @param options * The options map to use for formatting with the default code formatter. * Recognized options are documented on {@link JavaCore#getDefaultOptions()}. * If set to <code>null</code>, then use the current settings from {@link JavaCore#getOptions()}. * @return an TextEdit describing the changes required to format source * @throws IllegalArgumentException * If the offset and length are not inside the string, a IllegalArgumentException is thrown. */ public static TextEdit reformat(int kind, String source, int indentationLevel, String lineSeparator, Map<String, String> options) { return reformat(kind, source, 0, source.length(), indentationLevel, lineSeparator, options); }
protected void doFormatPreview() { fPreviewDocument.set(fPreviewText); String delimiter= TextUtilities.getDefaultLineDelimiter(fPreviewDocument); Map<String, String> prefs= fWorkingValues; if (fFormatterId != null) { prefs= new HashMap<>(fWorkingValues); prefs.put(JavaCore.JAVA_FORMATTER, fFormatterId); } fSourceViewer.setRedraw(false); try { TextEdit edit= CodeFormatterUtil.reformat(fCodeKind + CodeFormatter.F_INCLUDE_COMMENTS, fPreviewText, 0, delimiter, prefs); if (edit != null) edit.apply(fPreviewDocument); } catch (Exception e) { JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, FormatterMessages.JavaPreview_formatter_exception, e)); } finally { fSourceViewer.setRedraw(true); } }
@Override public void format() { super.format(); final IDocument document= fDocuments.removeFirst(); final TypedPosition partition= fPartitions.removeFirst(); final String path= fPaths.removeFirst(); if (document != null && partition != null) { Map<String, IDocumentPartitioner> partitioners= null; try { final boolean isModuleInfo= path != null && path.endsWith(JavaModelUtil.MODULE_INFO_JAVA); final int kind= (isModuleInfo ? CodeFormatter.K_MODULE_INFO : CodeFormatter.K_COMPILATION_UNIT) | CodeFormatter.F_INCLUDE_COMMENTS; final TextEdit edit= CodeFormatterUtil.reformat(kind, document.get(), partition.getOffset(), partition.getLength(), 0, TextUtilities.getDefaultLineDelimiter(document), getPreferences()); if (edit != null) { if (edit.getChildrenSize() > 20) partitioners= TextUtilities.removeDocumentPartitioners(document); edit.apply(document); } } catch (MalformedTreeException exception) { JavaPlugin.log(exception); } catch (BadLocationException exception) { // Can only happen on concurrent document modification - log and bail out JavaPlugin.log(exception); } finally { if (partitioners != null) TextUtilities.addDocumentPartitioners(document, partitioners); } } }
public void format() { super.format(); final IDocument document= (IDocument)fDocuments.removeFirst(); final TypedPosition partition= (TypedPosition)fPartitions.removeFirst(); if (document != null && partition != null) { Map partitioners= null; try { final TextEdit edit= CodeFormatterUtil.reformat(CodeFormatter.K_COMPILATION_UNIT, document.get(), partition.getOffset(), partition.getLength(), 0, TextUtilities.getDefaultLineDelimiter(document), getPreferences()); if (edit != null) { if (edit.getChildrenSize() > 20) partitioners= TextUtilities.removeDocumentPartitioners(document); edit.apply(document); } } catch (MalformedTreeException exception) { JavaPlugin.log(exception); } catch (BadLocationException exception) { // Can only happen on concurrent document modification - log and bail out JavaPlugin.log(exception); } finally { if (partitioners != null) TextUtilities.addDocumentPartitioners(document, partitioners); } } }
@Override public void format() { super.format(); final IDocument document= fDocuments.removeFirst(); final TypedPosition partition= fPartitions.removeFirst(); if (document != null && partition != null) { Map<String, IDocumentPartitioner> partitioners= null; try { final TextEdit edit= CodeFormatterUtil.reformat(CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, document.get(), partition.getOffset(), partition.getLength(), 0, TextUtilities.getDefaultLineDelimiter(document), getPreferences()); if (edit != null) { if (edit.getChildrenSize() > 20) partitioners= TextUtilities.removeDocumentPartitioners(document); edit.apply(document); } } catch (MalformedTreeException exception) { JavaPlugin.log(exception); } catch (BadLocationException exception) { // Can only happen on concurrent document modification - log and bail out JavaPlugin.log(exception); } finally { if (partitioners != null) TextUtilities.addDocumentPartitioners(document, partitioners); } } }
String lineDelimiter= TextUtilities.getDefaultLineDelimiter(document); TextEdit edit= CodeFormatterUtil.reformat(CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, content, regions, 0, lineDelimiter, formatterSettings); edit.apply(javaSourceViewer.getDocument());
edit= CodeFormatterUtil.reformat(CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, content, 0, lineDelemiter, formatterSettings); } else { if (regions.length == 0) return null; edit= CodeFormatterUtil.reformat(CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, content, regions, 0, lineDelemiter, formatterSettings);
Document document= new Document(content); TextEdit edit= CodeFormatterUtil.reformat(CodeFormatter.K_COMPILATION_UNIT, content, 0, TextUtilities.getDefaultLineDelimiter(document), fomatterSettings); if (edit == null || !edit.hasChildren()) return null;
int kind = (JavaModelUtil.isModuleInfo(compilationUnit) ? CodeFormatter.K_MODULE_INFO : CodeFormatter.K_COMPILATION_UNIT) | CodeFormatter.F_INCLUDE_COMMENTS; TextEdit edit= CodeFormatterUtil.reformat(kind, content, regions, 0, lineDelimiter, formatterSettings); edit.apply(javaSourceViewer.getDocument());