Move description dictionary to a new file
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import {complete, CompletionItem, DescriptionDictionary, trimTokenVector} from "./completion";
|
||||
import {complete, CompletionItem, trimTokenVector} from "./completion";
|
||||
import {DescriptionDictionary} from "./completion/descriptionDictionary";
|
||||
import {BooleanData} from "./data/boolean";
|
||||
import {Dictionary} from "./data/dictionary";
|
||||
import {StringData} from "./data/string";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {DescriptionPair} from "./completion/descriptionDictionary";
|
||||
import {Dictionary, isDictionary} from "./data/dictionary";
|
||||
import {ExpressionData, Kind, Pair} from "./data/expressiondata";
|
||||
import {ExpressionData} from "./data/expressiondata";
|
||||
import {Evaluator} from "./evaluator";
|
||||
import {wellKnownFunctions} from "./funcs";
|
||||
import {FunctionInfo} from "./funcs/info";
|
||||
@@ -12,36 +13,6 @@ export type CompletionItem = {
|
||||
function: boolean;
|
||||
};
|
||||
|
||||
export type DescriptionPair = Pair & {description?: string};
|
||||
|
||||
export class DescriptionDictionary extends Dictionary {
|
||||
private readonly descriptions = new Map<string, string>();
|
||||
|
||||
constructor(...pairs: DescriptionPair[]) {
|
||||
super();
|
||||
|
||||
for (const p of pairs) {
|
||||
this.add(p.key, p.value, p.description);
|
||||
}
|
||||
}
|
||||
|
||||
override add(key: string, value: ExpressionData, description?: string): void {
|
||||
super.add(key, value);
|
||||
if (description) {
|
||||
this.descriptions.set(key, description);
|
||||
}
|
||||
}
|
||||
|
||||
override pairs(): DescriptionPair[] {
|
||||
const pairs = super.pairs();
|
||||
return pairs.map(p => ({...p, description: this.descriptions.get(p.key)}));
|
||||
}
|
||||
}
|
||||
|
||||
export function isDescriptionDictionary(x: ExpressionData): x is DescriptionDictionary {
|
||||
return x.kind === Kind.Dictionary && x instanceof DescriptionDictionary;
|
||||
}
|
||||
|
||||
// Complete returns a list of completion items for the given expression.
|
||||
//
|
||||
// The main functionality is auto-completing functions and context access:
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import {StringData} from "../data";
|
||||
import {DescriptionDictionary} from "./descriptionDictionary";
|
||||
|
||||
describe("description dictionary", () => {
|
||||
it("pairs contains all values", () => {
|
||||
const d = new DescriptionDictionary();
|
||||
d.add("ABC", new StringData("val"));
|
||||
|
||||
expect(d.pairs()).toEqual([{key: "ABC", value: new StringData("val")}]);
|
||||
});
|
||||
|
||||
it("does not add duplicate entries", () => {
|
||||
const d = new DescriptionDictionary();
|
||||
d.add("ABC", new StringData("val1"));
|
||||
d.add("abc", new StringData("val2"));
|
||||
|
||||
expect(d.pairs()).toEqual([{key: "ABC", value: new StringData("val1")}]);
|
||||
});
|
||||
|
||||
it("can set optional descriptions", () => {
|
||||
const d = new DescriptionDictionary();
|
||||
d.add("ABC", new StringData("val"), "desc");
|
||||
d.add("DEF", new StringData("val"));
|
||||
|
||||
expect(d.pairs()).toEqual([
|
||||
{key: "ABC", value: new StringData("val"), description: "desc"},
|
||||
{key: "DEF", value: new StringData("val")}
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
import {Dictionary} from "../data/dictionary";
|
||||
import {ExpressionData, Kind, Pair} from "../data/expressiondata";
|
||||
|
||||
export type DescriptionPair = Pair & {description?: string};
|
||||
|
||||
export function isDescriptionDictionary(x: ExpressionData): x is DescriptionDictionary {
|
||||
return x.kind === Kind.Dictionary && x instanceof DescriptionDictionary;
|
||||
}
|
||||
|
||||
export class DescriptionDictionary extends Dictionary {
|
||||
private readonly descriptions = new Map<string, string>();
|
||||
|
||||
constructor(...pairs: DescriptionPair[]) {
|
||||
super();
|
||||
|
||||
for (const p of pairs) {
|
||||
this.add(p.key, p.value, p.description);
|
||||
}
|
||||
}
|
||||
|
||||
override add(key: string, value: ExpressionData, description?: string): void {
|
||||
super.add(key, value);
|
||||
if (description) {
|
||||
this.descriptions.set(key, description);
|
||||
}
|
||||
}
|
||||
|
||||
override pairs(): DescriptionPair[] {
|
||||
const pairs = super.pairs();
|
||||
return pairs.map(p => ({...p, description: this.descriptions.get(p.key)}));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
export {Expr} from "./ast";
|
||||
export {complete, CompletionItem, DescriptionDictionary, DescriptionPair, isDescriptionDictionary} from "./completion";
|
||||
export {complete, CompletionItem} from "./completion";
|
||||
export {DescriptionDictionary, DescriptionPair, isDescriptionDictionary} from "./completion/descriptionDictionary";
|
||||
export * as data from "./data";
|
||||
export {ExpressionError} from "./errors";
|
||||
export {Evaluator} from "./evaluator";
|
||||
|
||||
Reference in New Issue
Block a user