/** * Old API. Consider to use format2 (TextEdit) */ public static String format(int kind, String string, int indentationLevel, int[] positions, String lineSeparator, Map options) { return format(kind, string, 0, string.length(), indentationLevel, positions, lineSeparator, options); }
public static String format(int kind, String string, int indentationLevel, int[] positions, String lineSeparator, IJavaProject project) { Map options= project != null ? project.getOptions(true) : null; return format(kind, string, 0, string.length(), indentationLevel, positions, lineSeparator, options); }
/** * Old API. Consider to use format2 (TextEdit) * * @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 project * The project from which to retrieve the formatter options from * If set to <code>null</code>, then use the current settings from {@link JavaCore#getOptions()}. * @return the formatted source string */ public static String format(int kind, String source, int indentationLevel, String lineSeparator, IJavaProject project) { Map<String, String> options= project != null ? project.getOptions(true) : null; return format(kind, source, indentationLevel, lineSeparator, options); }
/** * Old API. Consider to use format2 (TextEdit) * * @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 project * The project from which to retrieve the formatter options from * If set to <code>null</code>, then use the current settings from {@link JavaCore#getOptions()}. * @return the formatted source string */ public static String format(int kind, String source, int indentationLevel, String lineSeparator, IJavaProject project) { Map<String, String> options= project != null ? project.getOptions(true) : null; return format(kind, source, indentationLevel, lineSeparator, options); }
protected void doFormatPreview() { if (fSnippets.isEmpty()) { fPreviewDocument.set(""); //$NON-NLS-1$ return; } //This delimiter looks best for invisible characters final String delimiter= "\n"; //$NON-NLS-1$ final StringBuffer buffer= new StringBuffer(); for (final Iterator iter= fSnippets.iterator(); iter.hasNext();) { final PreviewSnippet snippet= (PreviewSnippet) iter.next(); String formattedSource; try { formattedSource= CodeFormatterUtil.format(snippet.kind, snippet.source, 0, null, delimiter, fWorkingValues); } catch (Exception e) { final IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, FormatterMessages.JavaPreview_formatter_exception, e); JavaPlugin.log(status); continue; } buffer.append(delimiter); buffer.append(formattedSource); buffer.append(delimiter); buffer.append(delimiter); } fPreviewDocument.set(buffer.toString()); }
@Override protected void doFormatPreview() { if (fSnippets.isEmpty()) { fPreviewDocument.set(""); //$NON-NLS-1$ return; } //This delimiter looks best for invisible characters final String delimiter= "\n"; //$NON-NLS-1$ final StringBuffer buffer= new StringBuffer(); for (final Iterator<PreviewSnippet> iter= fSnippets.iterator(); iter.hasNext();) { final PreviewSnippet snippet= iter.next(); String formattedSource; try { formattedSource= CodeFormatterUtil.format(snippet.kind, snippet.source, 0, delimiter, fWorkingValues); } catch (Exception e) { final IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, FormatterMessages.JavaPreview_formatter_exception, e); JavaPlugin.log(status); continue; } buffer.append(delimiter); buffer.append(formattedSource); buffer.append(delimiter); buffer.append(delimiter); } fPreviewDocument.set(buffer.toString()); }
/** * Appends everything up to the method name including * the opening parenthesis. * <p> * In case of {@link CompletionProposal#METHOD_REF_WITH_CASTED_RECEIVER} * it add cast. * </p> * * @param buffer the string buffer * @since 3.4 */ protected void appendMethodNameReplacement(StringBuffer buffer) { if (fProposal.getKind() == CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER) { String coreCompletion= String.valueOf(fProposal.getCompletion()); String lineDelimiter= TextUtilities.getDefaultLineDelimiter(getTextViewer().getDocument()); String replacement= CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, coreCompletion, 0, lineDelimiter, fInvocationContext.getProject()); buffer.append(replacement.substring(0, replacement.lastIndexOf('.') + 1)); } if (fProposal.getKind() != CompletionProposal.CONSTRUCTOR_INVOCATION) buffer.append(fProposal.getName()); FormatterPrefs prefs= getFormatterPrefs(); if (prefs.beforeOpeningParen) buffer.append(SPACE); buffer.append(LPAREN); }
/** * Appends everything up to the method name including * the opening parenthesis. * <p> * In case of {@link CompletionProposal#METHOD_REF_WITH_CASTED_RECEIVER} * it add cast. * </p> * * @param buffer the string buffer * @since 3.4 */ protected void appendMethodNameReplacement(StringBuffer buffer) { if (fProposal.getKind() == CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER) { String coreCompletion= String.valueOf(fProposal.getCompletion()); String lineDelimiter= TextUtilities.getDefaultLineDelimiter(getTextViewer().getDocument()); String replacement= CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, coreCompletion, 0, lineDelimiter, FormatterProfileManager.getProjectSettings(fInvocationContext.getProject())); buffer.append(replacement.substring(0, replacement.lastIndexOf('.') + 1)); } if (fProposal.getKind() != CompletionProposal.CONSTRUCTOR_INVOCATION) buffer.append(fProposal.getName()); FormatterPrefs prefs= getFormatterPrefs(); if (prefs.beforeOpeningParen) buffer.append(SPACE); buffer.append(LPAREN); }
String replacement = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, 0, lineDelim, fField.getJavaProject());
private String createAccessorCUSource(IProgressMonitor pm) throws CoreException { IProject project= getFileHandle(fAccessorPath).getProject(); String lineDelimiter= StubUtility.getLineDelimiterPreference(project); return CodeFormatterUtil.format(CodeFormatter.K_COMPILATION_UNIT, getUnformattedSource(pm), 0, lineDelimiter, fCu.getJavaProject()); }
private String createAccessorCUSource(IProgressMonitor pm) throws CoreException { IProject project= getFileHandle(fAccessorPath).getProject(); String lineDelimiter= StubUtility.getLineDelimiterPreference(project); return CodeFormatterUtil.format(CodeFormatter.K_COMPILATION_UNIT, getUnformattedSource(pm), 0, null, lineDelimiter, fCu.getJavaProject()); }
private String createAccessorCUSource(IProgressMonitor pm) throws CoreException { IProject project= getFileHandle(fAccessorPath).getProject(); String lineDelimiter= StubUtility.getLineDelimiterPreference(project); return CodeFormatterUtil.format(CodeFormatter.K_COMPILATION_UNIT, getUnformattedSource(pm), 0, lineDelimiter, FormatterProfileManager.getProjectSettings(fCu.getJavaProject())); }
/** * Adds a new accessor for the specified field. * * @param type the type * @param field the field * @param contents the contents of the accessor method * @param rewrite the list rewrite to use * @param insertion the insertion point * @throws JavaModelException if an error occurs */ private void addNewAccessor(final IType type, final IField field, final String contents, final ListRewrite rewrite, final ASTNode insertion) throws JavaModelException { final String delimiter= StubUtility.getLineDelimiterUsed(type); final MethodDeclaration declaration= (MethodDeclaration) rewrite.getASTRewrite().createStringPlaceholder(CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, contents, 0, null, delimiter, field.getJavaProject()), ASTNode.METHOD_DECLARATION); if (insertion != null) rewrite.insertBefore(declaration, insertion, null); else rewrite.insertLast(declaration, null); }
/** * Adds a new accessor for the specified field. * * @param type the type * @param field the field * @param contents the contents of the accessor method * @param rewrite the list rewrite to use * @param insertion the insertion point * @throws JavaModelException if an error occurs */ private void addNewAccessor(final IType type, final IField field, final String contents, final ListRewrite rewrite, final ASTNode insertion) throws JavaModelException { final String delimiter= StubUtility.getLineDelimiterUsed(type); final MethodDeclaration declaration= (MethodDeclaration) rewrite.getASTRewrite().createStringPlaceholder(CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, contents, 0, delimiter, field.getJavaProject()), ASTNode.METHOD_DECLARATION); if (insertion != null) rewrite.insertBefore(declaration, insertion, null); else rewrite.insertLast(declaration, null); }
protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException { CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fField.getJavaProject()); boolean addComments= settings.createComments; int flags= Flags.AccPublic | (fField.getFlags() & Flags.AccStatic); String stub; if (fIsGetter) { String getterName= GetterSetterUtil.getGetterName(fField, null); stub= GetterSetterUtil.getGetterStub(fField, getterName, addComments, flags); } else { String setterName= GetterSetterUtil.getSetterName(fField, null); stub= GetterSetterUtil.getSetterStub(fField, setterName, addComments, flags); } // use the code formatter String lineDelim= TextUtilities.getDefaultLineDelimiter(document); IRegion region= document.getLineInformationOfOffset(getReplacementOffset()); int lineStart= region.getOffset(); int indent= Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth); String replacement= CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, null, lineDelim, fField.getJavaProject()); if (replacement.endsWith(lineDelim)) { replacement= replacement.substring(0, replacement.length() - lineDelim.length()); } setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement)); return true; }
/** * Adds a new accessor for the specified field. * * @param type the type * @param field the field * @param contents the contents of the accessor method * @param rewrite the list rewrite to use * @param insertion the insertion point * @throws JavaModelException if an error occurs */ private void addNewAccessor(final IType type, final IField field, final String contents, final ListRewrite rewrite, final ASTNode insertion) throws JavaModelException { final String delimiter= StubUtility.getLineDelimiterUsed(type); final MethodDeclaration declaration= (MethodDeclaration) rewrite.getASTRewrite().createStringPlaceholder( CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, contents, 0, delimiter, FormatterProfileManager.getProjectSettings(field.getJavaProject())), ASTNode.METHOD_DECLARATION); if (insertion != null) rewrite.insertBefore(declaration, insertion, null); else rewrite.insertLast(declaration, null); }
@Override protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException { CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fField.getJavaProject()); boolean addComments= settings.createComments; int flags= Flags.AccPublic | (fField.getFlags() & Flags.AccStatic); String stub; if (fIsGetter) { String getterName= GetterSetterUtil.getGetterName(fField, null); stub= GetterSetterUtil.getGetterStub(fField, getterName, addComments, flags); } else { String setterName= GetterSetterUtil.getSetterName(fField, null); stub= GetterSetterUtil.getSetterStub(fField, setterName, addComments, flags); } // use the code formatter String lineDelim= TextUtilities.getDefaultLineDelimiter(document); IRegion region= document.getLineInformationOfOffset(getReplacementOffset()); int lineStart= region.getOffset(); int indent= Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth); String replacement= CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, lineDelim, FormatterProfileManager.getProjectSettings(fField.getJavaProject())); if (replacement.endsWith(lineDelim)) { replacement= replacement.substring(0, replacement.length() - lineDelim.length()); } setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement)); return true; }
/** * Creates the Java completion proposal for the JDT Core * {@link CompletionProposal#FIELD_REF_WITH_CASTED_RECEIVER} proposal. * * @param proposal the JDT Core proposal * @return the Java completion proposal * @since 3.4 */ private IJavaCompletionProposal createFieldWithCastedReceiverProposal(CompletionProposal proposal) { String completion= String.valueOf(proposal.getCompletion()); completion= CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, completion, 0, "\n", fJavaProject); //$NON-NLS-1$ int start= proposal.getReplaceStart(); int length= getLength(proposal); StyledString label= fLabelProvider.createStyledLabel(proposal); Image image= getImage(fLabelProvider.createFieldImageDescriptor(proposal)); int relevance= computeRelevance(proposal); JavaCompletionProposal javaProposal= new JavaFieldWithCastedReceiverCompletionProposal(completion, start, length, image, label, relevance, getContext().isInJavadoc(), getInvocationContext(), proposal); if (fJavaProject != null) javaProposal.setProposalInfo(new FieldProposalInfo(fJavaProject, proposal)); javaProposal.setTriggerCharacters(VAR_TRIGGER); return javaProposal; }
private void insertMethod(IField field, ListRewrite rewrite, AccessorKind kind) throws CoreException { IType type = field.getDeclaringType(); String delimiter = StubUtility.getLineDelimiterUsed(type); int flags = generateVisibility | (field.getFlags() & Flags.AccStatic); String stub; if (kind == AccessorKind.GETTER) { String name = GetterSetterUtil.getGetterName(field, null); stub = GetterSetterUtil.getGetterStub(field, name, generateComments, flags); } else { String name = GetterSetterUtil.getSetterName(field, null); stub = GetterSetterUtil.getSetterStub(field, name, generateComments, flags); } String formattedStub = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, 0, delimiter, type.getJavaProject().getOptions(true)); MethodDeclaration declaration = (MethodDeclaration) rewrite.getASTRewrite().createStringPlaceholder(formattedStub, ASTNode.METHOD_DECLARATION); rewrite.insertLast(declaration, null); }
/** * Creates the Java completion proposal for the JDT Core * {@link CompletionProposal#FIELD_REF_WITH_CASTED_RECEIVER} proposal. * * @param proposal the JDT Core proposal * @return the Java completion proposal * @since 3.4 */ private IJavaCompletionProposal createFieldWithCastedReceiverProposal(CompletionProposal proposal) { String completion= String.valueOf(proposal.getCompletion()); completion= CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, completion, 0, "\n", FormatterProfileManager.getProjectSettings(fJavaProject)); //$NON-NLS-1$ int start= proposal.getReplaceStart(); int length= getLength(proposal); StyledString label= fLabelProvider.createStyledLabel(proposal); Image image= getImage(fLabelProvider.createFieldImageDescriptor(proposal)); int relevance= computeRelevance(proposal); JavaCompletionProposal javaProposal= new JavaFieldWithCastedReceiverCompletionProposal(completion, start, length, image, label, relevance, getContext().isInJavadoc(), getInvocationContext(), proposal); if (fJavaProject != null) javaProposal.setProposalInfo(new FieldProposalInfo(fJavaProject, proposal)); javaProposal.setTriggerCharacters(VAR_TRIGGER); return javaProposal; }