Skip to content

Commit

Permalink
Ensure line delimiter exists after the file header template.
Browse files Browse the repository at this point in the history
- When a file header template is defined there should be a line
  delimiter between it and the package declaration (or type declaration)
- Use project configured line delimiter instead of '\n' where possible

Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Oct 17, 2023
1 parent c4cc201 commit 9621530
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ CompletionContext getCompletionContext() {
return completionContext;
}

String getPackageHeader() throws JavaModelException {
String getPackageHeader(String lineDelimiter) throws JavaModelException {
if (packageHeader == null) {
IPackageDeclaration[] packageDeclarations = cu.getPackageDeclarations();
String packageName = cu.getParent().getElementName();
packageHeader = ((packageName != null && !packageName.isEmpty()) && (packageDeclarations == null || packageDeclarations.length == 0)) ? "package " + packageName + ";\n\n" : "";
packageHeader = ((packageName != null && !packageName.isEmpty()) && (packageDeclarations == null || packageDeclarations.length == 0)) ? "package " + packageName + ";" + lineDelimiter + lineDelimiter : "";
}
return packageHeader;
}
Expand Down Expand Up @@ -630,9 +630,10 @@ private static String getSnippetContent(SnippetCompletionContext scc, CodeGenera
}
CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), scc.getRecommendedLineSeprator());

String fileComment = cu.getTypes().length == 0 ? CodeGeneration.getFileComment(cu, StubUtility.getLineDelimiterUsed(cu.getJavaProject())) : null;
context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment + "\n" : "");
context.setVariable(PACKAGEHEADER, scc.getPackageHeader());
String lineDelimiter = StubUtility.getLineDelimiterUsed(cu.getJavaProject());
String fileComment = cu.getTypes().length == 0 ? CodeGeneration.getFileComment(cu, lineDelimiter) : null;
context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment + lineDelimiter : "");
context.setVariable(PACKAGEHEADER, scc.getPackageHeader(lineDelimiter));
String typeName = JavaCore.removeJavaLikeExtension(cu.getElementName());
List<IType> types = Arrays.asList(cu.getAllTypes());
int postfix = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ private CompilationUnitChange constructNewCUChange(ICompilationUnit cu) throws C

private String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
String fileComment = CodeGeneration.getFileComment(cu, lineDelimiter);
// Ensure separation after (optional) file comment
if (fileComment != null && !fileComment.isEmpty()) {
fileComment += lineDelimiter;
}
String typeComment = CodeGeneration.getTypeComment(cu, cu.getElementName(), lineDelimiter);
IPackageFragment pack = (IPackageFragment) cu.getParent();
String content = CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter);
Expand Down

0 comments on commit 9621530

Please sign in to comment.