-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Still WIP
- Loading branch information
Showing
4 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
server/gx-workflow-ls-format2/src/services/completionService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { ASTNodeManager } from "@gxwf/server-common/src/ast/nodeManager"; | ||
import { ASTNode } from "@gxwf/server-common/src/ast/types"; | ||
import { | ||
CompletionItem, | ||
CompletionItemKind, | ||
CompletionList, | ||
Position, | ||
TextDocument, | ||
} from "@gxwf/server-common/src/languageTypes"; | ||
import { TextBuffer } from "@gxwf/yaml-language-service/src/utils/textBuffer"; | ||
import { SchemaNode, SchemaNodeResolver } from "../schema"; | ||
|
||
export class GxFormat2CompletionService { | ||
constructor(protected readonly schemaNodeResolver: SchemaNodeResolver) {} | ||
|
||
public doComplete( | ||
textDocument: TextDocument, | ||
position: Position, | ||
nodeManager: ASTNodeManager | ||
): Promise<CompletionList> { | ||
const result: CompletionList = { | ||
items: [], | ||
isIncomplete: false, | ||
}; | ||
|
||
const textBuffer = new TextBuffer(textDocument); | ||
const text = textBuffer.getText(); | ||
const offset = textBuffer.getOffsetAt(position); | ||
const node = nodeManager.getNodeFromOffset(offset); | ||
if (!node) { | ||
return Promise.resolve(result); | ||
} | ||
if (text.charAt(offset - 1) === ":") { | ||
return Promise.resolve(result); | ||
} | ||
|
||
DEBUG_printNodeName(node); | ||
|
||
const existing = nodeManager.getDeclaredPropertyNames(node); | ||
if (nodeManager.isRoot(node)) { | ||
result.items = this.getProposedItems(this.schemaNodeResolver.rootNode, existing); | ||
return Promise.resolve(result); | ||
} | ||
const nodePath = nodeManager.getPathFromNode(node); | ||
const schemaNode = this.schemaNodeResolver.resolveSchemaContext(nodePath); | ||
if (schemaNode) { | ||
result.items = this.getProposedItems(schemaNode, existing); | ||
} | ||
return Promise.resolve(result); | ||
} | ||
|
||
private getProposedItems(schemaNode: SchemaNode, exclude: Set<string>): CompletionItem[] { | ||
const result: CompletionItem[] = []; | ||
schemaNode.children.forEach((child) => { | ||
if (exclude.has(child.name)) return; | ||
const item: CompletionItem = { | ||
label: child.name, | ||
documentation: child.documentation, | ||
sortText: `_${child.name}`, | ||
kind: CompletionItemKind.Field, | ||
insertText: `${child.name}: `, | ||
}; | ||
result.push(item); | ||
}); | ||
return result; | ||
} | ||
} | ||
|
||
function DEBUG_printNodeName(node: ASTNode): void { | ||
let nodeName = "_root_"; | ||
if (node?.type === "property") { | ||
nodeName = node.keyNode.value; | ||
console.debug("COMPLETION NODE PROPERTY", nodeName); | ||
} else if (node?.type === "object") { | ||
console.debug(`COMPLETION NODE OBJECT:`); | ||
node.properties.forEach((p) => { | ||
console.debug(` ${p.keyNode.value}`); | ||
}); | ||
} else { | ||
console.debug("UNKNOWN"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters