Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…a-dev-tools into oas-client
  • Loading branch information
KavinduZoysa committed Jan 6, 2025
2 parents b7487cd + f4133ac commit c66ba5e
Show file tree
Hide file tree
Showing 56 changed files with 3,618 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,21 @@ public ExpressionEditorContext(WorkspaceManager workspaceManager, Info info, Pat
this.filePath = filePath;
this.flowNode = gson.fromJson(info.node(), FlowNode.class);
this.document = document;
imports = getImportDeclarationNodes(document);
}

public ExpressionEditorContext(WorkspaceManager workspaceManager, Path filePath, Document document) {
this.workspaceManager = workspaceManager;
this.filePath = filePath;
this.document = document;
this.info = null;
this.flowNode = null;
imports = getImportDeclarationNodes(document);
}

private static List<ImportDeclarationNode> getImportDeclarationNodes(Document document) {
SyntaxTree syntaxTree = document.syntaxTree();
imports = syntaxTree.rootNode().kind() == SyntaxKind.MODULE_PART
return syntaxTree.rootNode().kind() == SyntaxKind.MODULE_PART
? ((ModulePartNode) syntaxTree.rootNode()).imports().stream().toList()
: List.of();
}
Expand Down Expand Up @@ -127,7 +139,7 @@ private Optional<TextEdit> getImport() {
return getImport(CommonUtils.getImportStatement(org, module, module));
}

private Optional<TextEdit> getImport(String importStatement) {
public Optional<TextEdit> getImport(String importStatement) {
try {
this.workspaceManager.loadProject(filePath);
} catch (WorkspaceDocumentException | EventSyncException e) {
Expand Down Expand Up @@ -202,15 +214,11 @@ public LineRange generateStatement() {
TextDocument textDocument = document.textDocument();
int textPosition = textDocument.textPositionFrom(info.startLine());

// Generate the statement
// Generate the statement and apply the text edits
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(textEdits.toArray(new TextEdit[0])));
applyContent(newTextDocument);
applyTextEdits(textEdits);

// Return the line range of the generated statement
LinePosition startLine = info.startLine();
Expand All @@ -230,6 +238,17 @@ public Position getCursorPosition() {
statementLineRange.startLine().offset() + info.offset() + expressionOffset);
}

/**
* Applies a list of text edits to the current document and updates the content.
*
* @param textEdits the list of text edits to be applied
*/
public void applyTextEdits(List<TextEdit> textEdits) {
TextDocument newTextDocument = document.textDocument()
.apply(TextDocumentChange.from(textEdits.toArray(new TextEdit[0])));
applyContent(newTextDocument);
}

/**
* Applies the content of the given TextDocument to the current document.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com)
* Copyright (c) 2025, 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
Expand Down Expand Up @@ -187,10 +187,11 @@ private List<GenSrcFile> genClientSourceFiles(io.ballerina.compiler.syntax.tree.
}

private String getTomlEntry(String module) {
return "[[tool.openapi]]" + LS +
"id" + " = " + module + LS +
"targetModule" + " = " + module + LS +
"filePath" + " = " + oAContractPath.toAbsolutePath() + LS;
String moduleWithQuotes = "\"" + module + "\"";
return LS + "[[tool.openapi]]" + LS +
"id" + " = " + moduleWithQuotes + LS +
"targetModule" + " = " + moduleWithQuotes + LS +
"filePath" + " = " + "\"" + oAContractPath.toAbsolutePath() + "\"" + LS;
}

private record ClientSource(boolean isModuleExists, Map<Path, List<TextEdit>> textEditsMap) {
Expand Down
Loading

0 comments on commit c66ba5e

Please sign in to comment.