Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…llerina-dev-tools into open-api-support
  • Loading branch information
KavinduZoysa committed Sep 26, 2024
2 parents e4540f5 + 1e628b7 commit d9f1f14
Show file tree
Hide file tree
Showing 28 changed files with 1,605 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode;
import io.ballerina.compiler.syntax.tree.StartActionNode;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.TemplateExpressionNode;
import io.ballerina.compiler.syntax.tree.Token;
import io.ballerina.compiler.syntax.tree.TransactionStatementNode;
import io.ballerina.compiler.syntax.tree.TypedBindingPatternNode;
Expand All @@ -99,6 +100,7 @@
import io.ballerina.flowmodelgenerator.core.model.node.Return;
import io.ballerina.flowmodelgenerator.core.model.node.Start;
import io.ballerina.flowmodelgenerator.core.model.node.UpdateData;
import io.ballerina.flowmodelgenerator.core.model.node.XMLPayload;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.LineRange;
import io.ballerina.tools.text.TextDocument;
Expand Down Expand Up @@ -384,6 +386,17 @@ private void checkForNewConnection(NewExpressionNode newExpressionNode,
}
}

@Override
public void visit(TemplateExpressionNode templateExpressionNode) {
if (templateExpressionNode.kind() == SyntaxKind.XML_TEMPLATE_EXPRESSION) {
startNode(FlowNode.Kind.XML_PAYLOAD)
.metadata()
.description(XMLPayload.DESCRIPTION)
.stepOut()
.properties().expression(templateExpressionNode);
}
}

@Override
public void visit(VariableDeclarationNode variableDeclarationNode) {
Optional<ExpressionNode> initializer = variableDeclarationNode.initializer();
Expand All @@ -407,6 +420,8 @@ public void visit(VariableDeclarationNode variableDeclarationNode) {
// TODO: Find a better way on how we can achieve this
if (nodeBuilder instanceof DataMapper) {
nodeBuilder.properties().data(variableDeclarationNode.typedBindingPattern());
} else if (nodeBuilder instanceof XMLPayload) {
nodeBuilder.properties().xmlPayload(variableDeclarationNode.typedBindingPattern());
} else {
nodeBuilder.properties().dataVariable(variableDeclarationNode.typedBindingPattern());
}
Expand Down Expand Up @@ -445,6 +460,9 @@ public void visit(AssignmentStatementNode assignmentStatementNode) {
.variable(assignmentStatementNode.varRef());
}

if (nodeBuilder instanceof XMLPayload) {
nodeBuilder.properties().variable(assignmentStatementNode.varRef());
}
endNode(assignmentStatementNode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,5 @@ public static String getVariableName(Node node) {
}
return node.toString().strip();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public enum Kind {
BODY,
NEW_DATA,
UPDATE_DATA,
XML_PAYLOAD,
STOP,
FOREACH,
DATA_MAPPER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.ballerina.flowmodelgenerator.core.model.node.Transaction;
import io.ballerina.flowmodelgenerator.core.model.node.UpdateData;
import io.ballerina.flowmodelgenerator.core.model.node.While;
import io.ballerina.flowmodelgenerator.core.model.node.XMLPayload;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.LineRange;
import org.ballerinalang.langserver.commons.workspace.WorkspaceManager;
Expand Down Expand Up @@ -125,6 +126,7 @@ public abstract class NodeBuilder {
put(FlowNode.Kind.FAIL, Fail::new);
put(FlowNode.Kind.NEW_DATA, NewData::new);
put(FlowNode.Kind.UPDATE_DATA, UpdateData::new);
put(FlowNode.Kind.XML_PAYLOAD, XMLPayload::new);
put(FlowNode.Kind.STOP, Stop::new);
put(FlowNode.Kind.FUNCTION_CALL, FunctionCall::new);
put(FlowNode.Kind.FOREACH, Foreach::new);
Expand Down Expand Up @@ -311,6 +313,28 @@ public PropertiesBuilder<T> dataVariable(Node node) {
return this;
}

public PropertiesBuilder<T> xmlPayload(Node node) {
data(node);

propertyBuilder
.metadata()
.label(Property.DATA_TYPE_LABEL)
.description(Property.DATA_TYPE_DOC)
.stepOut()
.type(Property.ValueType.TYPE)
.editable();

if (node == null) {
propertyBuilder.value("xml");
} else {
Optional<TypeSymbol> optTypeSymbol = CommonUtils.getTypeSymbol(semanticModel, node);
optTypeSymbol.ifPresent(typeSymbol -> propertyBuilder.value(
CommonUtils.getTypeSignature(semanticModel, typeSymbol, true, defaultModuleName)));
}
addProperty(Property.DATA_TYPE_KEY, propertyBuilder.build());
return this;
}

public PropertiesBuilder<T> data(Node node) {
Property property = propertyBuilder
.metadata()
Expand Down Expand Up @@ -538,6 +562,20 @@ public PropertiesBuilder<T> condition(ExpressionNode expressionNode) {
return this;
}

public PropertiesBuilder<T> expression(String expr, String expressionDoc) {
Property property = propertyBuilder
.metadata()
.label(Property.EXPRESSION_DOC)
.description(expressionDoc)
.stepOut()
.value(expr)
.type(Property.ValueType.EXPRESSION)
.editable()
.build();
addProperty(Property.EXPRESSION_KEY, property);
return this;
}

public PropertiesBuilder<T> expression(ExpressionNode expressionNode, String expressionDoc) {
Property property = propertyBuilder
.metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ public Map<Path, List<TextEdit>> toSource(SourceBuilder sourceBuilder) {

@Override
public void setConcreteTemplateData(TemplateContext context) {
properties().expression(null, FAIL_EXPRESSION_DOC);
properties().expression("", FAIL_EXPRESSION_DOC);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ public Map<Path, List<TextEdit>> toSource(SourceBuilder sourceBuilder) {
@Override
public void setConcreteTemplateData(TemplateContext context) {
metadata().description(String.format(DESCRIPTION, "name", "var"));
properties().dataVariable(null).expression(null, NEW_DATA_EXPRESSION_DOC);
properties().dataVariable(null).expression("", NEW_DATA_EXPRESSION_DOC);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ public Map<Path, List<TextEdit>> toSource(SourceBuilder sourceBuilder) {

@Override
public void setConcreteTemplateData(TemplateContext context) {
properties().expression(null, PANIC_EXPRESSION_DOC);
properties().expression("", PANIC_EXPRESSION_DOC);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ public Map<Path, List<TextEdit>> toSource(SourceBuilder sourceBuilder) {

@Override
public void setConcreteTemplateData(TemplateContext context) {
properties().expression(null, RETURN_EXPRESSION_DOC);
properties().expression("", RETURN_EXPRESSION_DOC);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ public Map<Path, List<TextEdit>> toSource(SourceBuilder sourceBuilder) {

@Override
public void setConcreteTemplateData(TemplateContext context) {
properties().expression(null, START_EXPRESSION_DOC);
properties().expression("", START_EXPRESSION_DOC);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public Map<Path, List<TextEdit>> toSource(SourceBuilder sourceBuilder) {
@Override
public void setConcreteTemplateData(TemplateContext context) {
metadata().description(String.format(DESCRIPTION, "name"));
properties().data(null).expression(null, UPDATE_DATA_EXPRESSION_DOC);
properties().data(null).expression("", UPDATE_DATA_EXPRESSION_DOC);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com)
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.flowmodelgenerator.core.model.node;

import io.ballerina.flowmodelgenerator.core.model.FlowNode;
import io.ballerina.flowmodelgenerator.core.model.NodeBuilder;
import io.ballerina.flowmodelgenerator.core.model.Property;
import io.ballerina.flowmodelgenerator.core.model.SourceBuilder;
import org.eclipse.lsp4j.TextEdit;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* Represents the properties of a xml payload node.
*
* @since 1.4.0
*/
public class XMLPayload extends NodeBuilder {

public static final String LABEL = "XML Payload";
public static final String DESCRIPTION = LABEL;
public static final String XML_PAYLOAD_DOC = "Create new XML payload";
private static final String DUMMY_XML_PAYLOAD = "xml `<dummy>Dummy XML value</dummy>`";

@Override
public void setConcreteConstData() {
metadata().label(LABEL);
codedata().node(FlowNode.Kind.XML_PAYLOAD);
}

@Override
public void setConcreteTemplateData(TemplateContext context) {
properties().xmlPayload(null)
.expression(DUMMY_XML_PAYLOAD, XML_PAYLOAD_DOC);
}

@Override
public Map<Path, List<TextEdit>> toSource(SourceBuilder sourceBuilder) {
sourceBuilder.newVariable();

Optional<Property> exprProperty = sourceBuilder.flowNode.getProperty(Property.EXPRESSION_KEY);
exprProperty.ifPresent(value -> sourceBuilder.token().expression(value).endOfStatement());

return sourceBuilder.textEdit(false).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com)
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/


package io.ballerina.flowmodelgenerator.extension;

import io.ballerina.flowmodelgenerator.extension.request.ExpressionEditorCompletionRequest;
import io.ballerina.projects.Document;
import io.ballerina.tools.text.TextDocument;
import io.ballerina.tools.text.TextDocumentChange;
import io.ballerina.tools.text.TextEdit;
import io.ballerina.tools.text.TextRange;
import org.ballerinalang.annotation.JavaSPIService;
import org.ballerinalang.langserver.commons.service.spi.ExtendedLanguageServerService;
import org.ballerinalang.langserver.commons.workspace.WorkspaceManager;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.CompletionParams;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest;
import org.eclipse.lsp4j.jsonrpc.services.JsonSegment;
import org.eclipse.lsp4j.services.LanguageServer;

import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@JavaSPIService("org.ballerinalang.langserver.commons.service.spi.ExtendedLanguageServerService")
@JsonSegment("expressionEditor")
public class ExpressionEditorService implements ExtendedLanguageServerService {

private WorkspaceManager workspaceManager;
private LanguageServer langServer;

@Override
public void init(LanguageServer langServer, WorkspaceManager workspaceManager) {
this.workspaceManager = workspaceManager;
this.langServer = langServer;
}

@Override
public Class<?> getRemoteInterface() {
return null;
}

@JsonRequest
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(
ExpressionEditorCompletionRequest request) {
return CompletableFuture.supplyAsync(() -> {
try {
// Load the project
Path filePath = Path.of(request.filePath());
this.workspaceManager.loadProject(filePath);
Path projectPath = this.workspaceManager.projectRoot(filePath);

// Create a temporary directory and load the project
ProjectCacheManager projectCacheManager = new ProjectCacheManager(projectPath, filePath);
projectCacheManager.createTempDirectory();
Path destination = projectCacheManager.getDestination();
this.workspaceManager.loadProject(destination);

// Get the document
Optional<Document> document = this.workspaceManager.document(destination);
if (document.isEmpty()) {
return Either.forLeft(List.of());
}
TextDocument textDocument = document.get().textDocument();

// Determine the cursor position
int textPosition = textDocument.textPositionFrom(request.startLine());
String statement = String.format("_ = %s;%n", request.expression());
TextEdit textEdit = TextEdit.from(TextRange.from(textPosition, 0), statement);
TextDocument newTextDocument =
textDocument.apply(TextDocumentChange.from(List.of(textEdit).toArray(new TextEdit[0])));
projectCacheManager.writeContent(newTextDocument);
document.get().modify()
.withContent(String.join(System.lineSeparator(), newTextDocument.textLines()))
.apply();

// Generate the completion params
Position position =
new Position(request.startLine().line(), request.startLine().offset() + 4 + request.offset());
TextDocumentIdentifier identifier = new TextDocumentIdentifier(destination.toUri().toString());
CompletionParams params = new CompletionParams(identifier, position, request.context());

// Get the completions
CompletableFuture<Either<List<CompletionItem>, CompletionList>> completableFuture =
langServer.getTextDocumentService().completion(params);
Either<List<CompletionItem>, CompletionList> completions = completableFuture.join();
projectCacheManager.deleteCache();
return completions;
} catch (Throwable e) {
return Either.forLeft(List.of());
}
});
}
}
Loading

0 comments on commit d9f1f14

Please sign in to comment.