Skip to content

Commit

Permalink
Completion at path segment end (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfhandl authored Jul 28, 2022
1 parent 25696f5 commit 3029cbb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
8 changes: 7 additions & 1 deletion tools/odataUri/src/autocomplete-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,13 @@ export class AutoCompleteManager {
} else {
// path
const suggestions = this.pathAutoComplete.getCompletions(pos);
return { from: input.lastIndexOf("/") + 1, suggestions };
const lastSlash = input.lastIndexOf("/");
const lastSegment = input.substring(lastSlash + 1);
if (suggestions.includes(lastSegment)) {
return { from: pos, suggestions: ["?", "/"] };
} else {
return { from: lastSlash + 1, suggestions };
}
}
}
}
56 changes: 31 additions & 25 deletions tools/odataUri/test/autocomplete-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ describe("odataUri", () => {
expectCompletions(manager, "/com", ["competitors", "company"], 1);
});

it("completions for path", () => {
const manager = new AutoCompleteManager(jetsons);
expectCompletions(manager, "company", ["?", "/"], 7);
expectCompletions(manager, "company/employees", ["?", "/"], 17);
});

it("completions for singleton", () => {
const manager = new AutoCompleteManager(jetsons);
expectCompletions(
Expand All @@ -39,7 +45,7 @@ describe("odataUri", () => {
"$top", //TODO: should not be suggested
"$skip", //TODO: should not be suggested] });
],
7
8
);
expectCompletions(
manager,
Expand All @@ -58,7 +64,7 @@ describe("odataUri", () => {
"$skip",
//TODO: $orderby should also be suggested
],
17
18
);
});

Expand All @@ -75,7 +81,7 @@ describe("odataUri", () => {
"$skip",
//TODO: $orderby should also be suggested
],
11
12
);
expectCompletions(
manager,
Expand Down Expand Up @@ -106,7 +112,7 @@ describe("odataUri", () => {
const errors = manager.getErrors("foo");
expect(errors).to.be.empty;

expectCompletions(manager, "competitors?", [], 11);
expectCompletions(manager, "competitors?", [], 12);

manager.updateSchema(jetsons);
expectCompletions(
Expand All @@ -120,59 +126,59 @@ describe("odataUri", () => {
"$skip",
//TODO: $orderby should also be suggested
],
11
12
);
});

it("completions for $select", () => {
const manager = new AutoCompleteManager(jetsons);
expectCompletions(manager, "competitors?$select", ["="], 18);
expectCompletions(manager, "competitors?$select", ["="], 19);
expectCompletions(
manager,
"competitors?$select=",
["stockSymbol", "name", "incorporated", "employees"],
19
20
);
expectCompletions(
manager,
"competitors?$select=stockSymbol",
["&", "stockSymbol", "name", "incorporated", "employees", ","],
30
31
);
});

it("completions for $expand", () => {
const manager = new AutoCompleteManager(jetsons);
expectCompletions(manager, "company?$expand", ["="], 14);
expectCompletions(manager, "company?$expand=", ["employees"], 15);
expectCompletions(manager, "company?$expand", ["="], 15);
expectCompletions(manager, "company?$expand=", ["employees"], 16);
expectCompletions(
manager,
"company?$expand=employees",
["&", "employees", ","],
24
25
);
});

it("completions for $filter", () => {
const manager = new AutoCompleteManager(jetsons);
expectCompletions(manager, "company?$filter", ["="], 14);
expectCompletions(manager, "company?$filter", ["="], 15);
expectCompletions(
manager,
"company?$filter=",
["stockSymbol", "name", "incorporated", "employees", "true", "false"],
15
16
);
expectCompletions(
manager,
"company?$filter=stocksymbol",
["eq", "ne", "gt", "ge", "lt", "le", "and", "or", "&"],
26
27
);
expectCompletions(
manager,
"company?$filter=stocksymbol eq",
["stockSymbol", "name", "incorporated", "employees", "true", "false"],
29
30
);
expectCompletions(
manager,
Expand All @@ -188,7 +194,7 @@ describe("odataUri", () => {
"or",
"&",
],
33
34
);
expectCompletions(
manager,
Expand All @@ -204,13 +210,13 @@ describe("odataUri", () => {
"or",
"&",
],
35
36
);
expectCompletions(
manager,
"company?$filter=(stocksymbol eq true",
[], //TODO: would expect closing paren
35
36
);
});

Expand All @@ -230,7 +236,7 @@ describe("odataUri", () => {
"employees", //TODO: that doesn't make much sense
"employees desc", //TODO: that doesn't make much sense
],
16
17
);
expectCompletions(
manager,
Expand All @@ -247,19 +253,19 @@ describe("odataUri", () => {
"employees desc", //TODO: that doesn't make much sense
//TODO: would have expected "," here
],
20
21
);
});

it("completions for $skip & $top", () => {
const manager = new AutoCompleteManager(jetsons);
// "competitors?$skip": [???], //TODO: throws exception
expectCompletions(manager, "competitors?$skip=", ["NUMBER"], 17);
expectCompletions(manager, "competitors?$skip=3", ["&"], 18);
expectCompletions(manager, "competitors?$skip=", ["NUMBER"], 18);
expectCompletions(manager, "competitors?$skip=3", ["&"], 19);

// "competitors?$top": [???], //TODO: throws exception
expectCompletions(manager, "competitors?$top=", ["NUMBER"], 16);
expectCompletions(manager, "competitors?$top=3", ["&"], 17);
expectCompletions(manager, "competitors?$top=", ["NUMBER"], 17);
expectCompletions(manager, "competitors?$top=3", ["&"], 18);
});

it("errors for invalid input", () => {
Expand Down Expand Up @@ -298,7 +304,7 @@ function expectCompletions(
suggestions: string[],
from: number = 0
) {
const actual = manager.getCompletions(input, input.length - 1);
const actual = manager.getCompletions(input, input.length);
expect(actual.suggestions).to.eql(suggestions, input);
expect(actual.from).to.eql(from, input);
}

0 comments on commit 3029cbb

Please sign in to comment.