Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure line delimiter exists after the file header template. #2906

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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