private void computeLines() { try { final String lineSeparator = this.compilationUnit.findRecommendedLineSeparator(); int fromIndex = 0; Matcher matcher = Pattern.compile(".*?" + lineSeparator).matcher(this.text); while (fromIndex < this.text.length() && matcher.find(fromIndex)) { String lineText = matcher.group(); int offset = this.text.indexOf(lineText, fromIndex); int length = lineText.length(); this.lines.add(new Line(lineText, offset, length, this)); fromIndex += offset + length; } } catch (JavaModelException e) { throw new UnhandledException(astRoot, e); } } }
private String removeIndentAndNewLines(String code, ICompilationUnit cu) throws JavaModelException { IJavaProject project = cu.getJavaProject(); Map options = project.getOptions(true/*inherit JavaCore options*/); int tabWidth = IndentManipulation.getTabWidth(options); int indentWidth = IndentManipulation.getIndentWidth(options); int indent = IndentManipulation.measureIndentUnits(code, tabWidth, indentWidth); int firstNonWhiteSpace = -1; int length = code.length(); while (firstNonWhiteSpace < length-1) if (!ScannerHelper.isWhitespace(code.charAt(++firstNonWhiteSpace))) break; int lastNonWhiteSpace = length; while (lastNonWhiteSpace > 0) if (!ScannerHelper.isWhitespace(code.charAt(--lastNonWhiteSpace))) break; String lineDelimiter = cu.findRecommendedLineSeparator(); return IndentManipulation.changeIndent(code.substring(firstNonWhiteSpace, lastNonWhiteSpace+1), indent, tabWidth, indentWidth, "", lineDelimiter); //$NON-NLS-1$ } /*
private String removeIndentAndNewLines(String code, ICompilationUnit cu) throws JavaModelException { IJavaProject project = cu.getJavaProject(); Map options = project.getOptions(true/*inherit JavaCore options*/); int tabWidth = IndentManipulation.getTabWidth(options); int indentWidth = IndentManipulation.getIndentWidth(options); int indent = IndentManipulation.measureIndentUnits(code, tabWidth, indentWidth); int firstNonWhiteSpace = -1; int length = code.length(); while (firstNonWhiteSpace < length-1) if (!ScannerHelper.isWhitespace(code.charAt(++firstNonWhiteSpace))) break; int lastNonWhiteSpace = length; while (lastNonWhiteSpace > 0) if (!ScannerHelper.isWhitespace(code.charAt(--lastNonWhiteSpace))) break; String lineDelimiter = cu.findRecommendedLineSeparator(); return IndentManipulation.changeIndent(code.substring(firstNonWhiteSpace, lastNonWhiteSpace+1), indent, tabWidth, indentWidth, "", lineDelimiter); //$NON-NLS-1$ } /*
private String removeIndentAndNewLines(String code, ICompilationUnit cu) throws JavaModelException { IJavaProject project = cu.getJavaProject(); Map options = project.getOptions(true/*inherit JavaCore options*/); int tabWidth = IndentManipulation.getTabWidth(options); int indentWidth = IndentManipulation.getIndentWidth(options); int indent = IndentManipulation.measureIndentUnits(code, tabWidth, indentWidth); int firstNonWhiteSpace = -1; int length = code.length(); while (firstNonWhiteSpace < length-1) if (!ScannerHelper.isWhitespace(code.charAt(++firstNonWhiteSpace))) break; int lastNonWhiteSpace = length; while (lastNonWhiteSpace > 0) if (!ScannerHelper.isWhitespace(code.charAt(--lastNonWhiteSpace))) break; String lineDelimiter = cu.findRecommendedLineSeparator(); return IndentManipulation.changeIndent(code.substring(firstNonWhiteSpace, lastNonWhiteSpace+1), indent, tabWidth, indentWidth, "", lineDelimiter); //$NON-NLS-1$ } /*
private String removeIndentAndNewLines(String code, ICompilationUnit cu) throws JavaModelException { IJavaProject project = cu.getJavaProject(); Map options = project.getOptions(true/*inherit JavaCore options*/); int tabWidth = IndentManipulation.getTabWidth(options); int indentWidth = IndentManipulation.getIndentWidth(options); int indent = IndentManipulation.measureIndentUnits(code, tabWidth, indentWidth); int firstNonWhiteSpace = -1; int length = code.length(); while (firstNonWhiteSpace < length-1) if (!ScannerHelper.isWhitespace(code.charAt(++firstNonWhiteSpace))) break; int lastNonWhiteSpace = length; while (lastNonWhiteSpace > 0) if (!ScannerHelper.isWhitespace(code.charAt(--lastNonWhiteSpace))) break; String lineDelimiter = cu.findRecommendedLineSeparator(); return IndentManipulation.changeIndent(code.substring(firstNonWhiteSpace, lastNonWhiteSpace+1), indent, tabWidth, indentWidth, "", lineDelimiter); //$NON-NLS-1$ } /*
private String removeIndentAndNewLines(String code, ICompilationUnit cu) throws JavaModelException { IJavaProject project = cu.getJavaProject(); Map options = project.getOptions(true/*inherit JavaCore options*/); int tabWidth = IndentManipulation.getTabWidth(options); int indentWidth = IndentManipulation.getIndentWidth(options); int indent = IndentManipulation.measureIndentUnits(code, tabWidth, indentWidth); int firstNonWhiteSpace = -1; int length = code.length(); while (firstNonWhiteSpace < length-1) if (!ScannerHelper.isWhitespace(code.charAt(++firstNonWhiteSpace))) break; int lastNonWhiteSpace = length; while (lastNonWhiteSpace > 0) if (!ScannerHelper.isWhitespace(code.charAt(--lastNonWhiteSpace))) break; String lineDelimiter = cu.findRecommendedLineSeparator(); return IndentManipulation.changeIndent(code.substring(firstNonWhiteSpace, lastNonWhiteSpace+1), indent, tabWidth, indentWidth, "", lineDelimiter); //$NON-NLS-1$ } /*
private String removeIndentAndNewLines(String code, ICompilationUnit cu) throws JavaModelException { IJavaProject project = cu.getJavaProject(); Map options = project.getOptions(true/*inherit JavaCore options*/); int tabWidth = IndentManipulation.getTabWidth(options); int indentWidth = IndentManipulation.getIndentWidth(options); int indent = IndentManipulation.measureIndentUnits(code, tabWidth, indentWidth); int firstNonWhiteSpace = -1; int length = code.length(); while (firstNonWhiteSpace < length-1) if (!ScannerHelper.isWhitespace(code.charAt(++firstNonWhiteSpace))) break; int lastNonWhiteSpace = length; while (lastNonWhiteSpace > 0) if (!ScannerHelper.isWhitespace(code.charAt(--lastNonWhiteSpace))) break; String lineDelimiter = cu.findRecommendedLineSeparator(); return IndentManipulation.changeIndent(code.substring(firstNonWhiteSpace, lastNonWhiteSpace+1), indent, tabWidth, indentWidth, "", lineDelimiter); //$NON-NLS-1$ } /*
private static CompletionItem getInterfaceSnippet(ICompilationUnit cu, CompletionContext completionContext, boolean needsPublic, IProgressMonitor monitor) { if (!accept(cu, completionContext, false)) { return null; } if (monitor.isCanceled()) { return null; } final CompletionItem interfaceSnippetItem = new CompletionItem(); interfaceSnippetItem.setFilterText(INTERFACE_SNIPPET_LABEL); interfaceSnippetItem.setLabel(INTERFACE_SNIPPET_LABEL); interfaceSnippetItem.setSortText(SortTextHelper.convertRelevance(0)); try { if (needsPublic) { interfaceSnippetItem.setInsertText(getSnippetContent(cu, CodeGenerationTemplate.INTERFACESNIPPET_PUBLIC, cu.findRecommendedLineSeparator(), true)); } else { interfaceSnippetItem.setInsertText(getSnippetContent(cu, CodeGenerationTemplate.INTERFACESNIPPET_DEFAULT, cu.findRecommendedLineSeparator(), true)); } setFields(interfaceSnippetItem, cu); } catch (CoreException e) { JavaLanguageServerPlugin.log(e.getStatus()); return null; } return interfaceSnippetItem; }
private static CompletionItem getClassSnippet(ICompilationUnit cu, CompletionContext completionContext, boolean needsPublic, IProgressMonitor monitor) { if (!accept(cu, completionContext, true)) { return null; } if (monitor.isCanceled()) { return null; } final CompletionItem classSnippetItem = new CompletionItem(); classSnippetItem.setLabel(CLASS_SNIPPET_LABEL); classSnippetItem.setFilterText(CLASS_SNIPPET_LABEL); classSnippetItem.setSortText(SortTextHelper.convertRelevance(1)); try { if (needsPublic) { classSnippetItem.setInsertText(getSnippetContent(cu, CodeGenerationTemplate.CLASSSNIPPET_PUBLIC, cu.findRecommendedLineSeparator(), true)); } else { classSnippetItem.setInsertText(getSnippetContent(cu, CodeGenerationTemplate.CLASSSNIPPET_DEFAULT, cu.findRecommendedLineSeparator(), true)); } setFields(classSnippetItem, cu); } catch (CoreException e) { JavaLanguageServerPlugin.log(e.getStatus()); return null; } return classSnippetItem; }
this.lineDelimiter = cu.findRecommendedLineSeparator();
this.lineDelimiter = cu.findRecommendedLineSeparator();
@SuppressWarnings("unchecked") private void setReturnStatement(final MethodDeclaration stub, final IMethodBinding methodToOverride, final ITypeBinding declaringType, final AST ast, final ASTRewrite rewrite) throws JavaModelException, CoreException { Expression expression= ASTNodeFactory.newDefaultExpression(ast, stub.getReturnType2(), stub.getExtraDimensions()); if (expression != null) { ReturnStatement returnStatement= ast.newReturnStatement(); returnStatement.setExpression(expression); String delimiter= cunit.findRecommendedLineSeparator(); Map<String, String> options= fJavaProject.getOptions(true); String bodyStatement= ASTNodes.asFormattedString(returnStatement, 0, delimiter, options); String placeHolder= CodeGeneration.getMethodBodyContent(cunit, declaringType.getName(), methodToOverride.getName(), false, bodyStatement, delimiter); if (placeHolder != null) { ASTNode todoNode = rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT); stub.getBody().statements().clear(); stub.getBody().statements().add(todoNode); } } }
private static Block createMethodStub(final MethodDeclaration method, final AST ast, ICompilationUnit cu, ASTRewrite rewrite, String targetTypeName) { final Block body= ast.newBlock(); final Expression expression= ASTNodeFactory.newDefaultExpression(ast, method.getReturnType2(), method.getExtraDimensions()); try { String delimiter= cu.findRecommendedLineSeparator(); String bodyStatement= ""; //$NON-NLS-1$ if (expression != null) { final ReturnStatement returnStatement= ast.newReturnStatement(); returnStatement.setExpression(expression); bodyStatement= ASTNodes.asFormattedString(returnStatement, 0, delimiter, FormatterProfileManager.getProjectSettings(cu.getJavaProject())); } String placeHolder= CodeGeneration.getMethodBodyContent(cu, targetTypeName, method.getName().getIdentifier(), false, bodyStatement, delimiter); if (placeHolder != null) { ReturnStatement todoNode= (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT); body.statements().add(todoNode); } } catch (CoreException e) { // return empty body } return body; }
public static void createClassIdsForIcu(ICompilationUnit icu, Set<IType> types, IProgressMonitor monitor, IWorkingCopyManager workingCopyManager, IJavaEnvironmentProvider envProvider) throws CoreException { workingCopyManager.register(icu, null); IJavaEnvironment environment = envProvider.get(icu.getJavaProject()); IImportCollector collector = new CompilationUnitScopedImportCollector(new ImportCollector(environment), S2eUtils.getPackage(icu)); IBuffer buffer = icu.getBuffer(); Document sourceDoc = new Document(buffer.getContents()); MultiTextEdit multiEdit = new MultiTextEdit(); IImportValidator validator = new ImportValidator(collector); String nl = icu.findRecommendedLineSeparator(); for (IType t : types) { String newClassId = ClassIdGenerators.generateNewId(new ClassIdGenerationContext(t)); AnnotationNewOperation op = new AnnotationNewOperation(ScoutAnnotationSourceBuilderFactory.createClassId(newClassId), t); TextEdit edit = op.createEdit(validator, sourceDoc, nl); multiEdit.addChild(edit); monitor.worked(1); if (monitor.isCanceled()) { return; } } try { multiEdit.apply(sourceDoc); buffer.setContents(sourceDoc.get()); // create imports new ImportsCreateOperation(icu, collector).run(new NullProgressMonitor(), workingCopyManager); } catch (BadLocationException e) { SdkLog.warning("Could not update @ClassId annotations for compilation unit '{}'.", icu.getElementName(), e); } }
String lineSeparator = icu.findRecommendedLineSeparator(); Wellformer w = new Wellformer(lineSeparator, isRecursive());
createTryCatchStatement(fCUnit.getBuffer(), fCUnit.findRecommendedLineSeparator());
createTryCatchStatement(fCUnit.getBuffer(), fCUnit.findRecommendedLineSeparator());
createTryCatchStatement(fCUnit.getBuffer(), fCUnit.findRecommendedLineSeparator());
@Override public void run(IProgressMonitor monitor, IWorkingCopyManager workingCopyManager) throws CoreException { ICompilationUnit icu = m_declaringMember.getCompilationUnit(); workingCopyManager.register(icu, monitor); IJavaEnvironment env = ScoutSdkCore.createJavaEnvironment(m_declaringMember.getJavaProject()); IImportCollector collector = new CompilationUnitScopedImportCollector(new ImportCollector(env), S2eUtils.getPackage(icu)); Document doc = new Document(icu.getSource()); TextEdit edit = createEdit(new ImportValidator(collector), doc, icu.findRecommendedLineSeparator()); try { edit.apply(doc); IBuffer buffer = icu.getBuffer(); buffer.setContents(doc.get()); // create imports new ImportsCreateOperation(icu, collector).run(monitor, workingCopyManager); } catch (Exception e) { SdkLog.warning("could not add annotation to '{}'.", m_declaringMember.getElementName(), e); } }
result.addTextEditGroup(substituteDesc); MethodDeclaration mm = createNewMethod(selectedNodes, fCUnit.findRecommendedLineSeparator(), substituteDesc);