diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/SnippetCompletionProposal.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/SnippetCompletionProposal.java index e8f633da57..1cdf8b7429 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/SnippetCompletionProposal.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/SnippetCompletionProposal.java @@ -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; } @@ -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 types = Arrays.asList(cu.getAllTypes()); int postfix = 0; diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/NewCUProposal.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/NewCUProposal.java index 87d2bdd695..59e5121cbd 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/NewCUProposal.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/NewCUProposal.java @@ -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);