Skip to content

Commit

Permalink
Fixed end of line in completion.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Jan 25, 2024
1 parent c73d8c9 commit 8aa2ce0
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 Obeo.
* Copyright (c) 2020, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -254,7 +254,7 @@ private static CompletableFuture<Either<List<CompletionItem>, CompletionList>> c
canceler.checkCanceled();

// Acceleo provides an API to access completion proposals.
final AcceleoCompletor acceleoCompletor = new AcceleoCompletor();
final AcceleoCompletor acceleoCompletor = new AcceleoCompletor(System.lineSeparator());
final String source = acceleoTextDocument.getContents();
final int atIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(position,
source);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 Obeo.
* Copyright (c) 2020, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -183,6 +183,16 @@ private int getValue(ICompletionProposal proposal) {
*/
private final AcceleoCompletionProposalsProvider acceleoCompletionProposalProvider;

/**
* The new line {@link String}.
*/
private final String newLine;

/**
* The {@link AcceleoCodeTemplates} with {@link #newLine}.
*/
private final AcceleoCodeTemplates acceleoCodeTemplates;

/**
* The computed module name.
*/
Expand All @@ -200,15 +210,19 @@ private int getValue(ICompletionProposal proposal) {
* the (non-{@code null}) contextual {@link IQualifiedNameQueryEnvironment}.
* @param acceleoValidationResult
* the (non-{@code null}) contextual {@link IAcceleoValidationResult}.
* @param newLine
* the new line {@link String}
*/
public AcceleoAstCompletor(IQualifiedNameQueryEnvironment queryEnvironment,
IAcceleoValidationResult acceleoValidationResult) {
IAcceleoValidationResult acceleoValidationResult, String newLine) {
this.queryEnvironment = Objects.requireNonNull(queryEnvironment);
this.acceleoValidationResult = Objects.requireNonNull(acceleoValidationResult);

this.astCompletor = new AstCompletor(new CompletionServices(this.queryEnvironment));
this.aqlCompletionEngine = new QueryCompletionEngine(queryEnvironment);
this.acceleoCompletionProposalProvider = new AcceleoCompletionProposalsProvider();
this.acceleoCompletionProposalProvider = new AcceleoCompletionProposalsProvider(newLine);
this.newLine = newLine;
this.acceleoCodeTemplates = new AcceleoCodeTemplates(newLine);
}

/**
Expand Down Expand Up @@ -857,43 +871,39 @@ private List<AcceleoCompletionProposal> getAqlCompletionProposals(Map<String, Se
private List<AcceleoCompletionProposal> getBodyCompletionProposals(int column) {
final List<AcceleoCompletionProposal> res = new ArrayList<>();

String newLinePrefix = AcceleoCodeTemplates.NEWLINE;
String newLinePrefix = newLine;
for (int i = 0; i < column; i++) {
newLinePrefix += AcceleoCodeTemplates.SPACE;
}

res.add(new AcceleoCodeTemplateCompletionProposal("New For", "Inserts the following For Statement:"
+ AcceleoCompletionProposal.DESCRIPTION_NEWLINE
+ AcceleoCompletionProposal.DESCRIPTION_CODE_OPEN + AcceleoCodeTemplates.NEW_FOR_STATEMENT
+ AcceleoCompletionProposal.DESCRIPTION_CODE_CLOSE, AcceleoCodeTemplates.NEW_FOR_STATEMENT
.replaceAll(AcceleoCodeTemplates.NEWLINE, newLinePrefix),
AcceleoPackage.Literals.FOR_STATEMENT));
+ AcceleoCompletionProposal.DESCRIPTION_CODE_OPEN + acceleoCodeTemplates.newForStatement()
+ AcceleoCompletionProposal.DESCRIPTION_CODE_CLOSE, acceleoCodeTemplates.newForStatement()
.replaceAll(newLine, newLinePrefix), AcceleoPackage.Literals.FOR_STATEMENT));
res.add(new AcceleoCodeTemplateCompletionProposal("New If", "Inserts the following If Statement:"
+ AcceleoCompletionProposal.DESCRIPTION_NEWLINE
+ AcceleoCompletionProposal.DESCRIPTION_CODE_OPEN + AcceleoCodeTemplates.NEW_IF_STATEMENT
+ AcceleoCompletionProposal.DESCRIPTION_CODE_CLOSE, AcceleoCodeTemplates.NEW_IF_STATEMENT
.replaceAll(AcceleoCodeTemplates.NEWLINE, newLinePrefix),
AcceleoPackage.Literals.IF_STATEMENT));
+ AcceleoCompletionProposal.DESCRIPTION_CODE_OPEN + acceleoCodeTemplates.newIfStatement()
+ AcceleoCompletionProposal.DESCRIPTION_CODE_CLOSE, acceleoCodeTemplates.newIfStatement()
.replaceAll(newLine, newLinePrefix), AcceleoPackage.Literals.IF_STATEMENT));
res.add(new AcceleoCodeTemplateCompletionProposal("New Let", "Inserts the following Let Statement:"
+ AcceleoCompletionProposal.DESCRIPTION_NEWLINE
+ AcceleoCompletionProposal.DESCRIPTION_CODE_OPEN + AcceleoCodeTemplates.NEW_LET_STATEMENT
+ AcceleoCompletionProposal.DESCRIPTION_CODE_CLOSE, AcceleoCodeTemplates.NEW_LET_STATEMENT
.replaceAll(AcceleoCodeTemplates.NEWLINE, newLinePrefix),
AcceleoPackage.Literals.LET_STATEMENT));
+ AcceleoCompletionProposal.DESCRIPTION_CODE_OPEN + acceleoCodeTemplates.newLetStatement()
+ AcceleoCompletionProposal.DESCRIPTION_CODE_CLOSE, acceleoCodeTemplates.newLetStatement()
.replaceAll(newLine, newLinePrefix), AcceleoPackage.Literals.LET_STATEMENT));
res.add(new AcceleoCodeTemplateCompletionProposal("New File", "Inserts the following File Statement:"
+ AcceleoCompletionProposal.DESCRIPTION_NEWLINE
+ AcceleoCompletionProposal.DESCRIPTION_CODE_OPEN + AcceleoCodeTemplates.NEW_FILE_STATEMENT
+ AcceleoCompletionProposal.DESCRIPTION_CODE_CLOSE, AcceleoCodeTemplates.NEW_FILE_STATEMENT
.replaceAll(AcceleoCodeTemplates.NEWLINE, newLinePrefix),
AcceleoPackage.Literals.FILE_STATEMENT));
+ AcceleoCompletionProposal.DESCRIPTION_CODE_OPEN + acceleoCodeTemplates.newFileStatement()
+ AcceleoCompletionProposal.DESCRIPTION_CODE_CLOSE, acceleoCodeTemplates.newFileStatement()
.replaceAll(newLine, newLinePrefix), AcceleoPackage.Literals.FILE_STATEMENT));
res.add(new AcceleoCodeTemplateCompletionProposal("New Protected Area",
"Inserts the following Protected Area Statement:"
+ AcceleoCompletionProposal.DESCRIPTION_NEWLINE
+ AcceleoCompletionProposal.DESCRIPTION_CODE_OPEN
+ AcceleoCodeTemplates.NEW_PROTECTED_AREA_STATEMENT
+ AcceleoCompletionProposal.DESCRIPTION_CODE_CLOSE,
AcceleoCodeTemplates.NEW_PROTECTED_AREA_STATEMENT.replaceAll(AcceleoCodeTemplates.NEWLINE,
newLinePrefix), AcceleoPackage.Literals.PROTECTED_AREA));
+ AcceleoCompletionProposal.DESCRIPTION_CODE_OPEN + acceleoCodeTemplates
.newProtectedAreaStatement()
+ AcceleoCompletionProposal.DESCRIPTION_CODE_CLOSE, acceleoCodeTemplates
.newProtectedAreaStatement().replaceAll(newLine, newLinePrefix),
AcceleoPackage.Literals.PROTECTED_AREA));

return res;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2021 Obeo.
* Copyright (c) 2017, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -32,6 +32,21 @@
*/
public class AcceleoCompletor {

/**
* The new line {@link String}.
*/
private String newLine;

/**
* Constructor.
*
* @param newLine
* the new line {@link String}
*/
public AcceleoCompletor(String newLine) {
this.newLine = newLine;
}

/**
* The name space for completion.
*/
Expand Down Expand Up @@ -115,7 +130,7 @@ protected List<AcceleoCompletionProposal> getProposals(IQualifiedNameQueryEnviro
final List<AcceleoCompletionProposal> completionProposals = new ArrayList<>();

AcceleoAstCompletor acceleoSyntaxCompletor = new AcceleoAstCompletor(queryEnvironment,
acceleoValidationResult);
acceleoValidationResult, newLine);

completionProposals.addAll(acceleoSyntaxCompletor.getCompletion(computedModuleName, sourceFragment,
acceleoElementToComplete));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2021 Obeo.
* Copyright (c) 2020, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -53,13 +53,23 @@ public class AcceleoCompletionProposalsProvider extends AcceleoSwitch<List<Accel
* The {@link AcceleoCodeTemplateCompletionProposalsProvider} that provides code template completion
* proposals.
*/
private final AcceleoCodeTemplateCompletionProposalsProvider acceleoCodeTemplatesProvider = new AcceleoCodeTemplateCompletionProposalsProvider();
private final AcceleoCodeTemplateCompletionProposalsProvider acceleoCodeTemplatesProvider;

/**
* The computed module name.
*/
private String computedModuleName;

/**
* Constructor.
*
* @param newLine
* the new line {@link String}
*/
public AcceleoCompletionProposalsProvider(String newLine) {
this.acceleoCodeTemplatesProvider = new AcceleoCodeTemplateCompletionProposalsProvider(newLine);
}

/**
* Provides the syntactic and code template completion proposals for a position where the given Acceleo
* type is syntactically allowed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2021 Obeo.
* Copyright (c) 2020, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -67,23 +67,6 @@ public class AcceleoCodeTemplateCompletionProposalsProvider extends AcceleoSwitc
+ AcceleoCodeTemplates.NEW_IMPORT + CODE_CLOSE, AcceleoCodeTemplates.NEW_IMPORT,
AcceleoPackage.Literals.IMPORT);

/**
* Code template completion proposal for a new {@link ModuleElementDocumentation}.
*/
public static final AcceleoCodeTemplateCompletionProposal NEW_MODULE_ELEMENT_DOCUMENTATION = new AcceleoCodeTemplateCompletionProposal(
"New Module Element Documentation", "Inserts the following sample Module Element Documentation:\n"
+ AcceleoCodeTemplates.NEW_MODULE_ELEMENT_DOCUMENTATION,
AcceleoCodeTemplates.NEW_MODULE_ELEMENT_DOCUMENTATION,
AcceleoPackage.Literals.MODULE_ELEMENT_DOCUMENTATION);

/**
* Code template completion proposal for a new {@link ModuleDocumentation}.
*/
public static final AcceleoCodeTemplateCompletionProposal NEW_MODULE_DOCUMENTATION = new AcceleoCodeTemplateCompletionProposal(
"New Module Documentation", "Inserts the following sample Module Documentation:\n"
+ AcceleoCodeTemplates.NEW_MODULE_DOCUMENTATION,
AcceleoCodeTemplates.NEW_MODULE_DOCUMENTATION, AcceleoPackage.Literals.MODULE_DOCUMENTATION);

/**
* Code template completion proposal for a new {@link Query}.
*/
Expand All @@ -92,14 +75,6 @@ public class AcceleoCodeTemplateCompletionProposalsProvider extends AcceleoSwitc
+ AcceleoCodeTemplates.NEW_QUERY + CODE_CLOSE, AcceleoCodeTemplates.NEW_QUERY,
AcceleoPackage.Literals.QUERY);

/**
* Code template completion proposal for a new {@link Template}.
*/
public static final AcceleoCodeTemplateCompletionProposal NEW_TEMPLATE = new AcceleoCodeTemplateCompletionProposal(
"New Template", "Inserts the following sample Acceleo Template:" + NEWLINE + CODE_OPEN
+ AcceleoCodeTemplates.NEW_TEMPLATE + CODE_CLOSE, AcceleoCodeTemplates.NEW_TEMPLATE,
AcceleoPackage.Literals.TEMPLATE);

/**
* Code template completion proposal for a new {@link Comment}.
*/
Expand All @@ -108,14 +83,6 @@ public class AcceleoCodeTemplateCompletionProposalsProvider extends AcceleoSwitc
+ AcceleoCodeTemplates.NEW_COMMENT + CODE_CLOSE, AcceleoCodeTemplates.NEW_COMMENT,
AcceleoPackage.Literals.COMMENT);

/**
* Code template completion proposal for a new {@link BlockComment}.
*/
public static final AcceleoCodeTemplateCompletionProposal NEW_BLOCK_COMMENT = new AcceleoCodeTemplateCompletionProposal(
"New Block Comment", "Inserts the following sample Block Comment:" + NEWLINE + CODE_OPEN
+ AcceleoCodeTemplates.NEW_BLOCK_COMMENT + CODE_CLOSE,
AcceleoCodeTemplates.NEW_BLOCK_COMMENT, AcceleoPackage.Literals.BLOCK_COMMENT);

/**
* Code template completion proposal for a new '@main' {@link Comment}.
*/
Expand All @@ -129,6 +96,27 @@ public class AcceleoCodeTemplateCompletionProposalsProvider extends AcceleoSwitc
*/
private String computedModuleName;

/**
* The new line {@link String}.
*/
private final String newLine;

/**
* The {@link AcceleoCodeTemplates} with the {@link #newLine}.
*/
private final AcceleoCodeTemplates acceleoCodeTemplates;

/**
* Constructor.
*
* @param newLine
* the new line {@link String}
*/
public AcceleoCodeTemplateCompletionProposalsProvider(String newLine) {
this.newLine = newLine;
this.acceleoCodeTemplates = new AcceleoCodeTemplates(newLine);
}

/**
* Provides the code template completion proposals for a position where the given Acceleo type is
* syntactically allowed.
Expand All @@ -145,6 +133,47 @@ public List<AcceleoCompletionProposal> getProposalsFor(String computedModuleName
return this.doSwitch(acceleoEClass, null);
}

/**
* Code template completion proposal for a new {@link ModuleElementDocumentation}.
*/
public AcceleoCodeTemplateCompletionProposal newModuleElementDocumentation() {
return new AcceleoCodeTemplateCompletionProposal("New Module Element Documentation",
"Inserts the following sample Module Element Documentation:" + newLine + acceleoCodeTemplates
.newModuleElementDocumentation(), acceleoCodeTemplates
.newModuleElementDocumentation(),
AcceleoPackage.Literals.MODULE_ELEMENT_DOCUMENTATION);
}

/**
* Code template completion proposal for a new {@link ModuleDocumentation}.
*/
public AcceleoCodeTemplateCompletionProposal newModuleDocumentation() {
return new AcceleoCodeTemplateCompletionProposal("New Module Documentation",
"Inserts the following sample Module Documentation:" + newLine + acceleoCodeTemplates
.newModuleDocumentation(), acceleoCodeTemplates.newModuleDocumentation(),
AcceleoPackage.Literals.MODULE_DOCUMENTATION);
}

/**
* Code template completion proposal for a new {@link Template}.
*/
public AcceleoCodeTemplateCompletionProposal newTemplate() {
return new AcceleoCodeTemplateCompletionProposal("New Template",
"Inserts the following sample Acceleo Template:" + NEWLINE + CODE_OPEN + acceleoCodeTemplates
.newTemplate() + CODE_CLOSE, acceleoCodeTemplates.newTemplate(),
AcceleoPackage.Literals.TEMPLATE);
}

/**
* Code template completion proposal for a new {@link BlockComment}.
*/
public AcceleoCodeTemplateCompletionProposal newBlockComment() {
return new AcceleoCodeTemplateCompletionProposal("New Block Comment",
"Inserts the following sample Block Comment:" + NEWLINE + CODE_OPEN + acceleoCodeTemplates
.newBlockComment() + CODE_CLOSE, acceleoCodeTemplates.newBlockComment(),
AcceleoPackage.Literals.BLOCK_COMMENT);
}

@Override
public List<AcceleoCompletionProposal> defaultCase(EObject object) {
return new ArrayList<>();
Expand Down Expand Up @@ -172,7 +201,7 @@ public List<AcceleoCompletionProposal> caseQuery(Query object) {
public List<AcceleoCompletionProposal> caseTemplate(Template object) {
List<AcceleoCompletionProposal> completionProposals = new ArrayList<>();

completionProposals.add(NEW_TEMPLATE);
completionProposals.add(newTemplate());

return completionProposals;
}
Expand Down Expand Up @@ -214,7 +243,7 @@ public List<AcceleoCompletionProposal> caseComment(Comment object) {
public List<AcceleoCompletionProposal> caseBlockComment(BlockComment object) {
List<AcceleoCompletionProposal> completionProposals = new ArrayList<>();

completionProposals.add(NEW_BLOCK_COMMENT);
completionProposals.add(newBlockComment());

return completionProposals;
}
Expand All @@ -230,7 +259,7 @@ public List<AcceleoCompletionProposal> caseDocumentation(Documentation object) {
public List<AcceleoCompletionProposal> caseModuleDocumentation(ModuleDocumentation object) {
List<AcceleoCompletionProposal> completionProposals = new ArrayList<>();

completionProposals.add(NEW_MODULE_DOCUMENTATION);
completionProposals.add(newModuleDocumentation());

return completionProposals;
}
Expand All @@ -239,7 +268,7 @@ public List<AcceleoCompletionProposal> caseModuleDocumentation(ModuleDocumentati
public List<AcceleoCompletionProposal> caseModuleElementDocumentation(ModuleElementDocumentation object) {
List<AcceleoCompletionProposal> completionProposals = new ArrayList<>();

completionProposals.add(NEW_MODULE_ELEMENT_DOCUMENTATION);
completionProposals.add(newModuleElementDocumentation());

return completionProposals;
}
Expand Down
Loading

0 comments on commit 8aa2ce0

Please sign in to comment.