Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow apidom-ls strict filter #2954

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 14 additions & 0 deletions packages/apidom-ls/src/config/openapi/media-type/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ const completion: ApidomCompletionItem[] = [
{ namespace: 'openapi', version: '3.0.3' },
],
},
{
label: 'schema',
insertText: 'schema',
kind: 14,
format: CompletionFormat.OBJECT,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[Schema Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#schemaObject) | [Reference Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#referenceObject)\n\\\n\\\nThe schema defining the content of the request, response, or parameter.',
},
targetSpecs: [{ namespace: 'openapi', version: '3.1.0' }],
},
{
label: 'example',
insertText: 'example',
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