Skip to content

Commit

Permalink
feat: allow apidom-ls strict filter
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Jul 16, 2023
1 parent 47363af commit 352e53e
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 9 deletions.
35 changes: 32 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,37 @@ env:
CPU_CORES: 2

jobs:
lint:
lint-commit-messages:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # necessary for linting commit messages
ref: ${{ github.event.pull_request.head.sha }} # necessary to check out pull request HEAD commit instead of merge commit
- name: Use Node.js 20
uses: actions/setup-node@v3
with:
node-version: 20
cache: 'npm'
cache-dependency-path: 'package-lock.json'
- name: Install dependencies
run: npm ci
- name: Lint commit message
if: github.ref != 'refs/heads/main' && github.actor != 'dependabot[bot]'
run: |
echo PR COMMITS: ${{ github.event.pull_request.commits }}
echo LOG prev
git log -n 4 HEAD~${{ github.event.pull_request.commits }}
echo LOG head
git log -n 4 HEAD
npx commitlint --from HEAD~${{ github.event.pull_request.commits }} --to HEAD --verbose
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Use Node.js 20
uses: actions/setup-node@v3
with:
Expand All @@ -32,8 +56,13 @@ jobs:
run: npm run lint
- name: Lint commit message
if: github.ref != 'refs/heads/main' && github.actor != 'dependabot[bot]'
run: npx commitlint --from HEAD~${{ github.event.pull_request.commits }} --to HEAD

run: |
echo PR COMMITS: ${{ github.event.pull_request.commits }}
echo LOG prev
git log -n 4 HEAD~${{ github.event.pull_request.commits }}
echo LOG head
git log -n 4 HEAD
npx commitlint --from HEAD~${{ github.event.pull_request.commits + 1}} --to HEAD~1 --verbose
check-typescript-types:
runs-on: ubuntu-latest

Expand Down
1 change: 1 addition & 0 deletions packages/apidom-ls/src/apidom-language-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export interface ValidationContext {

export interface CompletionContext {
maxNumberOfItems?: number;
enableLSPFilter?: boolean;
}

export interface DerefContext {
Expand Down
29 changes: 23 additions & 6 deletions packages/apidom-ls/src/services/completion/completion-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class DefaultCompletionService implements CompletionService {
}

// eslint-disable-next-line class-methods-use-this
private resolveCaretContext(node: Element, offset: number): CaretContext {
private resolveCaretContext(node: Element, offset: number, textModified: boolean): CaretContext {
let caretContext: CaretContext = CaretContext.UNDEFINED;
if (node) {
const sm = getSourceMap(node);
Expand All @@ -180,7 +180,11 @@ export class DefaultCompletionService implements CompletionService {
if (offset > sm.offset && offset < sm.endOffset!) {
caretContext = CaretContext.KEY_INNER;
} else if (offset === sm.offset) {
caretContext = CaretContext.KEY_START;
if (sm.length === 0 && textModified) {
caretContext = CaretContext.KEY_END;
} else {
caretContext = CaretContext.KEY_START;
}
} else {
caretContext = CaretContext.KEY_END;
}
Expand Down Expand Up @@ -251,6 +255,7 @@ export class DefaultCompletionService implements CompletionService {
): Promise<CompletionList> {
perfStart(PerfLabels.START);
const context = !completionContext ? this.settings?.completionContext : completionContext;
const enableFiltering = context?.enableLSPFilter;
const completionList: CompletionList = {
items: [],
isIncomplete: false,
Expand Down Expand Up @@ -543,7 +548,7 @@ export class DefaultCompletionService implements CompletionService {
// only if we have a node
let completionNode: Element | undefined;
if (node) {
const caretContext = this.resolveCaretContext(node, targetOffset);
const caretContext = this.resolveCaretContext(node, targetOffset, textModified);
completionNode = this.resolveCompletionNode(node, caretContext);
const completionNodeContext = this.resolveCompletionNodeContext(caretContext);

Expand Down Expand Up @@ -707,7 +712,15 @@ export class DefaultCompletionService implements CompletionService {
} else if (contentLanguage.format === 'YAML') {
// item.insertText = `${item.insertText}\n`;
}
collector.add(item);
if (word && word.length > 0) {
if (enableFiltering && item.insertText?.includes(word)) {
collector.add(item);
} else if (!enableFiltering) {
collector.add(item);
}
} else if (!word) {
collector.add(item);
}
}
} else if (
// in a primitive value node
Expand Down Expand Up @@ -781,8 +794,12 @@ export class DefaultCompletionService implements CompletionService {
*/
item.filterText = text.substring(nodeSourceMap.offset, nodeSourceMap.endOffset!);

if (word && word.length > 0 && unquotedOriginalInsertText?.includes(word)) {
collector.add(item);
if (word && word.length > 0) {
if (enableFiltering && unquotedOriginalInsertText?.includes(word)) {
collector.add(item);
} else if (!enableFiltering) {
collector.add(item);
}
} else if (!word) {
collector.add(item);
}
Expand Down
80 changes: 80 additions & 0 deletions packages/apidom-ls/test/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1243,4 +1243,84 @@ describe('apidom-ls-complete', function () {
},
] as ApidomCompletionItem[]);
});

it('openapi / yaml - test LSP provided filter', async function () {
const completionContext: CompletionContext = {
maxNumberOfItems: 100,
enableLSPFilter: true,
};

const spec = fs
.readFileSync(path.join(__dirname, 'fixtures', 'openapi-complete-filter.yaml'))
.toString();

const doc: TextDocument = TextDocument.create(
'foo://bar/penapi-complete-filter.yaml',
'yaml',
0,
spec,
);

const pos = Position.create(4, 8);
const result = await languageService.doCompletion(
doc,
{ textDocument: doc, position: pos },
completionContext,
);
assert.deepEqual(result!.items, [
{
label: 'responses',
insertText: 'responses: \n $1',
kind: 14,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[Responses Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#responsesObject)\n\\\n\\\nThe list of possible responses as they are returned from executing this operation.',
},
targetSpecs: [{ namespace: 'openapi', version: '3.1.0' }],
filterText: 'se',
textEdit: {
range: { start: { line: 4, character: 6 }, end: { line: 4, character: 8 } },
newText: 'responses: \n $1',
},
},
{
label: 'security',
insertText: 'security: \n - $1',
kind: 14,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[[Security Requirement Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#serverObject)]\n\\\n\\\nA declaration of which security mechanisms can be used for this operation. The list of values includes alternative security requirement objects that can be used. Only one of the security requirement objects need to be satisfied to authorize a request. To make security optional, an empty security requirement (`{}`) can be included in the array. This definition overrides any declared top-level [`security`](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#oasSecurity). To remove a top-level security declaration, an empty array can be used.',
},
targetSpecs: [{ namespace: 'openapi', version: '3.1.0' }],
preselect: true,
filterText: 'se',
textEdit: {
range: { start: { line: 4, character: 6 }, end: { line: 4, character: 8 } },
newText: 'security: \n - $1',
},
},
{
label: 'servers',
insertText: 'servers: \n - $1',
kind: 14,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[[Server Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#serverObject)]\n\\\n\\\nAn alternative `server` array to service this operation. If an alternative `server` object is specified at the Path Item Object or Root level, it will be overridden by this value.',
},
targetSpecs: [{ namespace: 'openapi', version: '3.1.0' }],
preselect: true,
filterText: 'se',
textEdit: {
range: { start: { line: 4, character: 6 }, end: { line: 4, character: 8 } },
newText: 'servers: \n - $1',
},
},
] as ApidomCompletionItem[]);
});
});
6 changes: 6 additions & 0 deletions packages/apidom-ls/test/fixtures/openapi-complete-filter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
openapi: 3.1.0
paths:
/pet:
put:
se
summary: Update an existing pet

0 comments on commit 352e53e

Please sign in to comment.