-
Notifications
You must be signed in to change notification settings - Fork 3
/
WrapInProtectedCommand.java
85 lines (73 loc) · 3.54 KB
/
WrapInProtectedCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*******************************************************************************
* Copyright (c) 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
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.aql.ls.services.workspace.command;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.acceleo.AcceleoPackage;
import org.eclipse.acceleo.Expression;
import org.eclipse.acceleo.ProtectedArea;
import org.eclipse.acceleo.aql.completion.proposals.templates.AcceleoCodeTemplates;
import org.eclipse.acceleo.aql.ls.common.AcceleoLanguageServerPositionUtils;
import org.eclipse.acceleo.aql.ls.services.textdocument.AcceleoTextDocument;
import org.eclipse.acceleo.aql.parser.AcceleoAstSerializer;
import org.eclipse.acceleo.query.AQLUtils;
import org.eclipse.acceleo.query.AQLUtils.AcceleoAQLResult;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;
/**
* Wrap the selected statements in a {@link ProtectedArea}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class WrapInProtectedCommand extends AbstractDocumentRangeCommand {
public WorkspaceEdit exec(AcceleoTextDocument document, Range range) {
final WorkspaceEdit res;
if (range.getStart().getCharacter() == 0 && range.getEnd().getCharacter() == 0) {
final int startIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(range
.getStart(), document.getContents());
final int endIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(range
.getEnd(), document.getContents());
if (isSameBlock(document, startIndex, endIndex)) {
final Map<String, List<TextEdit>> changes = new LinkedHashMap<>();
final ProtectedArea statement = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createProtectedArea();
final Expression expression = AcceleoPackage.eINSTANCE.getAcceleoFactory().createExpression();
final AcceleoAQLResult aqlExpression = AQLUtils.parseWhileAqlExpression(
AcceleoCodeTemplates.DEFAULT_EXPRESSION);
expression.setAst(aqlExpression.getAstResult());
expression.setAql(aqlExpression.getAstResult().getAst());
statement.setId(expression);
// TODO pass the new line String
final String lineDelimiter = System.lineSeparator();
final String text = document.getContents().substring(startIndex, endIndex);
final String blockIndentation = getBlockIndentation(text);
statement.setBody(createBlock(text, blockIndentation, " ", lineDelimiter, false));
final String replacementText = blockIndentation + new AcceleoAstSerializer(lineDelimiter)
.serialize(statement);
final Range replacementRange = AcceleoLanguageServerPositionUtils.getCorrespondingRange(
startIndex, endIndex, document.getContents());
final TextEdit edit = new TextEdit(replacementRange, replacementText + lineDelimiter);
final String sourceURI = document.getQueryEnvironment().getLookupEngine().getResolver()
.getSourceURI(document.getModuleQualifiedName()).toString();
changes.put(sourceURI, Collections.singletonList(edit));
res = new WorkspaceEdit(changes);
} else {
res = null;
}
} else {
res = null;
}
return res;
}
}