Skip to content

Commit

Permalink
feat: Add completion/signature support for `PredefinedSignature.Boole…
Browse files Browse the repository at this point in the history
…anOrAuto`
  • Loading branch information
hangxingliu committed Apr 28, 2024
1 parent 2ae7b89 commit 3f83a24
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
22 changes: 14 additions & 8 deletions src/docs/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import { createMarkdown } from "../utils/vscode";
import { ExtensionConfig } from "../config/vscode-config-loader";
import { SignatureInformation } from "vscode";
import { PredefinedSignature } from "../hint-data/types-manifest";
import { RequiredDirectiveCompletionItem } from "../hint-data/types-runtime";

export function genSignaturesCore(
directive: Readonly<Pick<RequiredDirectiveCompletionItem, "signatures">>,
config: Readonly<Pick<ExtensionConfig, "booleanStyle">>
): ReadonlyArray<string> {
if (directive.signatures === PredefinedSignature.Boolean) return [config.booleanStyle.split("-").join("|")];
if (directive.signatures === PredefinedSignature.BooleanOrAuto)
return [config.booleanStyle.split("-").concat("auto").join("|")];
if (isNonEmptyArray(directive.signatures)) return directive.signatures;
return [];
}

export function genSignatureDocsForDirective(
managers: HintDataManagers,
Expand All @@ -32,17 +44,11 @@ export function genSignatureDocsForDirective(
if (directive.fix?.help) docsMarkdown += directive.fix.help;
else docsMarkdown += docs ? docs.str.value : "";

const signStrings: string[] = [];
if (directive.signatures === PredefinedSignature.Boolean) {
signStrings.push(config.booleanStyle.split("-").join("|"));
} else if (isNonEmptyArray(directive.signatures)) {
signStrings.push(...signStrings);
}

let signStrings = genSignaturesCore(directive, config);
if (!signStrings[0]) {
let defaultSign = `${directive.directiveName}=`;
if (section) defaultSign = `[${section.name}] ${defaultSign}`;
signStrings[0] = defaultSign;
signStrings = [defaultSign];
}
for (const signString of signStrings)
signatures.push(new SignatureInformation(signString, createMarkdown(docsMarkdown)));
Expand Down
4 changes: 3 additions & 1 deletion src/hint-data/manager/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export class HintDataManager {
if (signatures) {
if (signatures === PredefinedSignature.Boolean) {
label.detail = " boolean";
} else if (signatures === PredefinedSignature.BooleanOrAuto) {
label.detail = " boolean|auto";
} else {
label.detail = " " + signatures;
}
Expand Down Expand Up @@ -229,7 +231,7 @@ export class HintDataManager {
}
if (item.fixHelp || renames[i]) {
extraProps.fix = {
help: item.fixHelp || item.docs || '',
help: item.fixHelp || item.docs || "",
url: item.fixURL,
rename: renames[i],
};
Expand Down
17 changes: 9 additions & 8 deletions src/vscode-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export class SystemdCompletionProvider implements CompletionItemProvider {
"@",
];
private fileTypeToSections: Array<Array<CompletionItem>> = [];
private booleanItems: CompletionItem[] | undefined;
private podmanEnabled: boolean;
constructor(private readonly config: ExtensionConfig, private readonly managers: HintDataManagers) {
this.podmanEnabled = config.podmanCompletion;
Expand All @@ -68,22 +67,21 @@ export class SystemdCompletionProvider implements CompletionItemProvider {
this.podmanEnabled = this.config.podmanCompletion;
this.fileTypeToSections = [];
}
this.booleanItems = undefined;
};

private readonly extendsValueEnum: ValueEnumExtendsFn = (rule) => {
if (rule.extends === PredefinedSignature.Boolean) return this.getBooleanCompletion();
if (rule.extends === PredefinedSignature.Boolean) return this.getBooleanCompletion(false);
if (rule.extends === PredefinedSignature.BooleanOrAuto) return this.getBooleanCompletion(true);
};

getBooleanCompletion() {
if (this.booleanItems) return this.booleanItems;
getBooleanCompletion(withAuto: boolean) {
const enums = this.config.booleanStyle.split("-");
this.booleanItems = enums.map((it, index) => {
if (withAuto) enums.push("auto");
return enums.map((it, index) => {
const ci = new CompletionItem(it, CompletionItemKind.Keyword);
ci.sortText = `bool${index}`;
return ci;
});
return this.booleanItems;
}

provideCompletionItems(
Expand Down Expand Up @@ -158,7 +156,10 @@ export class SystemdCompletionProvider implements CompletionItemProvider {
const list = this.managers.getDirectiveList(directive, { section, file });
if (list && list.length > 0) {
const directive = list[0];
if (directive.signatures === PredefinedSignature.Boolean) return this.getBooleanCompletion();
if (directive.signatures === PredefinedSignature.Boolean)
return this.getBooleanCompletion(false);
if (directive.signatures === PredefinedSignature.BooleanOrAuto)
return this.getBooleanCompletion(true);
}
}
return;
Expand Down

0 comments on commit 3f83a24

Please sign in to comment.