Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into service-model
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Dec 16, 2024
2 parents 1c44b2f + dc01386 commit 37b5f8e
Show file tree
Hide file tree
Showing 55 changed files with 6,899 additions and 4,286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ private void addRemainingParamsToPropertyMap(LinkedHashMap<String, ParameterResu
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.placeholder(paramResult.defaultValue())
.typeConstraint(paramResult.type())
Expand Down Expand Up @@ -465,6 +466,7 @@ private void buildPropsFromFuncCallArgs(SeparatedNodeList<FunctionArgumentNode>
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.placeholder(paramResult.defaultValue())
.typeConstraint(paramResult.type())
Expand Down Expand Up @@ -534,6 +536,7 @@ private void buildPropsFromFuncCallArgs(SeparatedNodeList<FunctionArgumentNode>
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.stepOut()
.addProperty(unescapedParamName);
Expand Down Expand Up @@ -562,6 +565,7 @@ private void buildPropsFromFuncCallArgs(SeparatedNodeList<FunctionArgumentNode>
.codedata()
.kind(restParamResult.kind().name())
.originalName(restParamResult.name())
.importStatements(restParamResult.importStatements())
.stepOut()
.stepOut()
.addProperty(unescapedParamName);
Expand Down Expand Up @@ -618,6 +622,7 @@ private void buildPropsFromFuncCallArgs(SeparatedNodeList<FunctionArgumentNode>
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.stepOut()
.addProperty(unescapedParamName, paramValue);
Expand Down Expand Up @@ -647,6 +652,7 @@ private void buildPropsFromFuncCallArgs(SeparatedNodeList<FunctionArgumentNode>
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.stepOut()
.addProperty(unescapedParamName, paramValue);
Expand Down Expand Up @@ -676,6 +682,7 @@ private void buildPropsFromFuncCallArgs(SeparatedNodeList<FunctionArgumentNode>
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.stepOut()
.addProperty(unescapedParamName, paramValue);
Expand Down Expand Up @@ -707,6 +714,7 @@ private void buildPropsFromFuncCallArgs(SeparatedNodeList<FunctionArgumentNode>
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.stepOut()
.addProperty(unescapedParamName, paramValue);
Expand Down Expand Up @@ -736,6 +744,7 @@ private void buildPropsFromFuncCallArgs(SeparatedNodeList<FunctionArgumentNode>
.codedata()
.kind(includedRecordRest.kind().name())
.originalName(includedRecordRest.name())
.importStatements(includedRecordRest.importStatements())
.stepOut()
.stepOut()
.addProperty("additionalValues");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.ballerina.compiler.syntax.tree.IdentifierToken;
import io.ballerina.compiler.syntax.tree.ImportDeclarationNode;
import io.ballerina.compiler.syntax.tree.ModulePartNode;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
Expand All @@ -40,16 +39,15 @@
import io.ballerina.tools.text.TextDocumentChange;
import io.ballerina.tools.text.TextEdit;
import io.ballerina.tools.text.TextRange;
import org.ballerinalang.langserver.common.utils.CommonUtil;
import org.ballerinalang.langserver.commons.eventsync.exceptions.EventSyncException;
import org.ballerinalang.langserver.commons.workspace.WorkspaceDocumentException;
import org.ballerinalang.langserver.commons.workspace.WorkspaceManager;
import org.eclipse.lsp4j.Position;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Represents the context for the expression editor.
Expand Down Expand Up @@ -94,6 +92,9 @@ public Optional<Property> getProperty() {
}

public boolean isNodeKind(List<NodeKind> nodeKinds) {
if (flowNode == null || flowNode.codedata() == null) {
return false;
}
return nodeKinds.contains(flowNode.codedata().node());
}

Expand All @@ -116,45 +117,47 @@ public Info info() {
}

// TODO: Check how we can use SourceBuilder in place of this method
public Optional<TextEdit> getImport() {
private Optional<TextEdit> getImport() {
String org = flowNode.codedata().org();
String module = flowNode.codedata().module();

if (org == null || module == null || org.equals(CommonUtil.BALLERINA_ORG_NAME) &&
CommonUtil.PRE_DECLARED_LANG_LIBS.contains(module)) {
if (org == null || module == null || CommonUtils.isPredefinedLangLib(org, module)) {
return Optional.empty();
}
return getImport(CommonUtils.getImportStatement(org, module, module));
}

private Optional<TextEdit> getImport(String importStatement) {
try {
this.workspaceManager.loadProject(filePath);
} catch (WorkspaceDocumentException | EventSyncException e) {
return Optional.empty();
}

// Check if the import statement represents the current module
Optional<Module> currentModule = this.workspaceManager.module(filePath);
if (currentModule.isPresent()) {
ModuleDescriptor descriptor = currentModule.get().descriptor();
if (descriptor.org().value().equals(org) && descriptor.name().toString().equals(module)) {
if (CommonUtils.getImportStatement(descriptor.org().toString(), descriptor.packageName().value(),
descriptor.name().toString()).equals(importStatement)) {
return Optional.empty();
}
}

// Check if the import statement already exists
boolean importExists = imports.stream().anyMatch(importDeclarationNode -> {
String moduleName = importDeclarationNode.moduleName().stream()
.map(IdentifierToken::text)
.collect(Collectors.joining("."));
return importDeclarationNode.orgName().isPresent() &&
org.equals(importDeclarationNode.orgName().get().orgName().text()) &&
module.equals(moduleName);
String importText = importDeclarationNode.toSourceCode().trim();
return importText.startsWith("import " + importStatement) && importText.endsWith(";");
});

// Add the import statement
// Generate the import statement if not exists
if (!importExists) {
String importStatement = new SourceBuilder.TokenBuilder(null)
String stmt = new SourceBuilder.TokenBuilder(null)
.keyword(SyntaxKind.IMPORT_KEYWORD)
.name(flowNode.codedata().getImportSignature())
.name(importStatement)
.endOfStatement()
.build(false);
TextEdit textEdit = TextEdit.from(TextRange.from(0, 0), importStatement);
TextEdit textEdit = TextEdit.from(TextRange.from(0, 0), stmt);
return Optional.of(textEdit);
}
return Optional.empty();
Expand All @@ -167,22 +170,49 @@ public Optional<TextEdit> getImport() {
* @return the line range of the generated statement.
*/
public LineRange generateStatement() {
String prefix = getProperty()
.flatMap(property -> Optional.ofNullable(property.valueTypeConstraint()))
.map(obj -> obj + " _ = ")
.orElse("_ = ");
String prefix = "_ = ";
Optional<Property> optionalProperty = getProperty();
List<TextEdit> textEdits = new ArrayList<>();

String statement = String.format("%s%s;%n", prefix, info.expression());
this.expressionOffset = prefix.length();
if (optionalProperty.isPresent()) {
// Append the type if exists
Property property = optionalProperty.get();
if (property.valueTypeConstraint() != null) {
prefix = String.format("%s _ = ", property.valueTypeConstraint());
}

// Add the import statements of the dependent types
if (property.codedata() != null) {
String importStatements = property.codedata().importStatements();
if (importStatements != null && !importStatements.isEmpty()) {
for (String importStmt : importStatements.split(",")) {
getImport(importStmt).ifPresent(textEdits::add);
}
}
}
}

// Add the import statement for the node type
if (isNodeKind(List.of(NodeKind.NEW_CONNECTION, NodeKind.FUNCTION_CALL, NodeKind.REMOTE_ACTION_CALL,
NodeKind.RESOURCE_ACTION_CALL))) {
getImport().ifPresent(textEdits::add);
}

// Get the text position of the start line
TextDocument textDocument = document.textDocument();
int textPosition = textDocument.textPositionFrom(info.startLine());

TextEdit textEdit = TextEdit.from(TextRange.from(textPosition, 0), statement);
// Generate the statement
String statement = String.format("%s%s;%n", prefix, info.expression());
this.expressionOffset = prefix.length();
textEdits.add(TextEdit.from(TextRange.from(textPosition, 0), statement));

// Apply the text edits to the document
TextDocument newTextDocument = textDocument
.apply(TextDocumentChange.from(List.of(textEdit).toArray(new TextEdit[0])));
.apply(TextDocumentChange.from(textEdits.toArray(new TextEdit[0])));
applyContent(newTextDocument);

// Return the line range of the generated statement
LinePosition startLine = info.startLine();
LinePosition endLineRange = LinePosition.from(startLine.line(),
startLine.offset() + statement.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ public List<ParameterResult> getFunctionParameters(int functionId) {
"p.kind, " +
"p.optional, " +
"p.default_value, " +
"p.description " +
"p.description, " +
"p.import_statements " + // Added this line
"FROM Parameter p " +
"WHERE p.function_id = ?;";
try (Connection conn = DriverManager.getConnection(dbPath);
Expand All @@ -401,7 +402,9 @@ public List<ParameterResult> getFunctionParameters(int functionId) {
Parameter.Kind.valueOf(rs.getString("kind")),
rs.getString("default_value"),
rs.getString("description"),
rs.getInt("optional"));
rs.getInt("optional"),
rs.getString("import_statements")
);
parameterResults.add(parameterResult);
}
return parameterResults;
Expand All @@ -419,7 +422,8 @@ public LinkedHashMap<String, ParameterResult> getFunctionParametersAsMap(int fun
"p.kind, " +
"p.optional, " +
"p.default_value, " +
"p.description " +
"p.description, " +
"p.import_statements " + // Added this line
"FROM Parameter p " +
"WHERE p.function_id = ?;";
try (Connection conn = DriverManager.getConnection(dbPath);
Expand All @@ -436,7 +440,9 @@ public LinkedHashMap<String, ParameterResult> getFunctionParametersAsMap(int fun
Parameter.Kind.valueOf(rs.getString("kind")),
rs.getString("default_value"),
rs.getString("description"),
rs.getInt("optional"));
rs.getInt("optional"),
rs.getString("import_statements")
);
parameterResults.put(paramName, parameterResult);
}
return parameterResults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
/**
* Represents the result of a parameter.
*
* @param parameterId the ID of the parameter
* @param name the name of the parameter
* @param type the type of the parameter
* @param kind the kind of the parameter
* @param defaultValue the default value of the parameter
* @param description the description of the parameter
* @param optional whether the parameter is optional
* @param parameterId the ID of the parameter
* @param name the name of the parameter
* @param type the type of the parameter
* @param kind the kind of the parameter
* @param defaultValue the default value of the parameter
* @param description the description of the parameter
* @param optional whether the parameter is optional
* @param importStatements import statements of the dependent types
* @since 2.0.0
*/
public record ParameterResult(
Expand All @@ -37,5 +38,6 @@ public record ParameterResult(
Parameter.Kind kind,
String defaultValue,
String description,
Integer optional) {
Integer optional,
String importStatements) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public static ModuleInfo from(ModuleID moduleId) {

public static ModuleInfo from(ModuleDescriptor moduleDescriptor) {
return new ModuleInfo(moduleDescriptor.org().value(), moduleDescriptor.packageName().value(),
moduleDescriptor.name().moduleNamePart(), moduleDescriptor.version().value().toString());
moduleDescriptor.name().toString(), moduleDescriptor.version().value().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@
/**
* Represents the codedata of a property.
*
* @param kind The kind of the property
* @param originalName The original name of the property
* @param kind The kind of the property
* @param originalName The original name of the property
* @param importStatements import statements of the dependent types
* @since 2.0.0
*/
public record PropertyCodedata(String kind, String originalName) {
public record PropertyCodedata(String kind, String originalName, String importStatements) {

public static class Builder<T> extends FacetedBuilder<T> {

private String kind;
private String originalName;
private String importStatements;

public Builder(T parentBuilder) {
super(parentBuilder);
Expand All @@ -47,8 +49,13 @@ public Builder<T> originalName(String originalName) {
return this;
}

public Builder<T> importStatements(String importStatements) {
this.importStatements = importStatements;
return this;
}

public PropertyCodedata build() {
return new PropertyCodedata(kind, originalName);
return new PropertyCodedata(kind, originalName, importStatements);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void setConcreteTemplateData(TemplateContext context) {
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.placeholder(paramResult.defaultValue())
.typeConstraint(paramResult.type())
Expand Down Expand Up @@ -197,6 +198,7 @@ public void setConcreteTemplateData(TemplateContext context) {
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.placeholder(paramResult.defaultValue())
.typeConstraint(paramResult.type())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public void setConcreteTemplateData(TemplateContext context) {
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.placeholder(paramResult.defaultValue())
.typeConstraint(paramResult.type())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ private static FlowNode fetchNodeTemplate(NodeBuilder nodeBuilder, Codedata code
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.placeholder(paramResult.defaultValue())
.typeConstraint(paramResult.type())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public void setConcreteTemplateData(TemplateContext context) {
.codedata()
.kind(paramResult.kind().name())
.originalName(paramResult.name())
.importStatements(paramResult.importStatements())
.stepOut()
.placeholder(paramResult.defaultValue())
.typeConstraint(paramResult.type())
Expand Down
Loading

0 comments on commit 37b5f8e

Please sign in to comment.