Skip to content

Commit c8f33b2

Browse files
committed
feat(apidom-ls): - improve logic and update config
* Improve/fix completion, validation and documentation logic * Move asyncapi config from swagger-ide to apidom-ls * Complete rules for asyncapi schema section
1 parent 35f7496 commit c8f33b2

File tree

141 files changed

+6933
-1499
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+6933
-1499
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ trim_trailing_whitespace = true
2828
# Denotes whether file should end with a newline
2929
# Possible values - true, false
3030
insert_final_newline = true
31+
32+
[packages/apidom-ls/test/fixtures/**/*.json]
33+
trim_trailing_whitespace = false
34+
35+
[packages/apidom-ls/test/fixtures/**/*.yaml]
36+
trim_trailing_whitespace = false

packages/apidom-ls/src/apidom-language-service.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { DefaultDerefService } from './services/deref/deref-service';
2525
import { DefaultDefinitionService } from './services/definition/definition-service';
2626
import { getDocumentCache } from './document-cache';
2727
import { parse } from './parser-factory';
28+
import { config } from './config/config';
2829

2930
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3031
export default function getLanguageService(context: LanguageServiceContext): LanguageService {
@@ -46,20 +47,21 @@ export default function getLanguageService(context: LanguageServiceContext): Lan
4647
definitionService.configure(languageSettings);
4748
}
4849

50+
let metadata = config();
51+
if (context?.metadata) {
52+
metadata = context.metadata;
53+
}
4954
const documentCache = getDocumentCache<ParseResultElement>(10, 60, (document) =>
50-
parse(document, context?.metadata?.metadataMaps),
55+
parse(document, metadata.metadataMaps),
5156
);
5257

53-
// TODO solve init and config
54-
if (context.metadata) {
55-
const languageSettings: LanguageSettings = {
56-
metadata: context.metadata,
57-
validate: true,
58-
validatorProviders: context.validatorProviders,
59-
documentCache,
60-
};
61-
configureServices(languageSettings);
62-
}
58+
const languageSettings: LanguageSettings = {
59+
metadata,
60+
validate: true,
61+
validatorProviders: context?.validatorProviders,
62+
documentCache,
63+
};
64+
configureServices(languageSettings);
6365

6466
return {
6567
// eslint-disable-next-line @typescript-eslint/no-unused-vars

packages/apidom-ls/src/apidom-language-types.ts

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ import { Element, ParseResultElement } from '@swagger-api/apidom-core';
2828

2929
import { DocumentCache } from './document-cache';
3030

31-
// eslint-disable-next-line @typescript-eslint/naming-convention
32-
export enum SUPPORTED_LANGUAGES {
31+
export enum SupportedLanguages {
3332
OPENAPI_31,
3433
ASYNCAPI_20,
3534
}
3635

37-
export enum FORMAT {
36+
export enum Format {
3837
JSON,
3938
YAML,
4039
}
@@ -95,7 +94,7 @@ export interface CompletionContext {
9594
}
9695

9796
export interface DerefContext {
98-
format?: FORMAT;
97+
format?: Format;
9998
baseURI?: string;
10099
}
101100

@@ -112,9 +111,32 @@ export interface WorkspaceContextService {
112111
resolveRelativePath(relativePath: string, resource: string): string;
113112
}
114113

114+
export enum CompletionType {
115+
PROPERTY,
116+
VALUE,
117+
UNDEFINED,
118+
}
119+
120+
export enum CompletionFormat {
121+
UNQUOTED,
122+
QUOTED,
123+
QUOTED_FORCED,
124+
OBJECT,
125+
ARRAY,
126+
UNDEFINED,
127+
}
128+
115129
export interface ApidomCompletionItem extends CompletionItem {
116130
targetSpecs?: NamespaceVersion[];
117131
target?: string;
132+
arrayMember?: boolean;
133+
type?: CompletionType;
134+
format?: CompletionFormat;
135+
insertTextJson?: string;
136+
insertTextYaml?: string;
137+
conditions?: LinterCondition[];
138+
function?: string;
139+
functionParams?: [unknown] | undefined | unknown;
118140
}
119141

120142
export interface DocumentationMeta {
@@ -125,11 +147,6 @@ export interface DocumentationMeta {
125147
conditions?: LinterCondition[];
126148
}
127149

128-
export interface ElementMeta {
129-
completion?: ApidomCompletionItem[];
130-
validation?: string[];
131-
}
132-
133150
export interface QuickFixData {
134151
message: string;
135152
function?: string;
@@ -149,7 +166,7 @@ export interface LinterMeta {
149166
source?: string;
150167
severity?: 1 | 2 | 3 | 4 | undefined;
151168
linterFunction?: string;
152-
linterParams?: [any];
169+
linterParams?: [unknown] | undefined | unknown;
153170
marker?: string;
154171
markerTarget?: string;
155172
target?: string;
@@ -160,10 +177,10 @@ export interface LinterMeta {
160177
}
161178

162179
export interface LinterCondition {
163-
targets: LinterConditionTarget[];
180+
targets?: LinterConditionTarget[];
164181
function: string;
165182
negate?: boolean;
166-
params: [any];
183+
params?: [unknown] | undefined | unknown;
167184
}
168185

169186
export interface LinterConditionTarget {
@@ -172,7 +189,9 @@ export interface LinterConditionTarget {
172189
}
173190

174191
export interface FormatMeta {
175-
[index: string]: ElementMeta | LinterMeta[] | DocumentationMeta[];
192+
documentation?: DocumentationMeta[];
193+
lint?: LinterMeta[];
194+
completion?: ApidomCompletionItem[];
176195
}
177196

178197
export interface MetadataMap {
@@ -192,8 +211,17 @@ export interface LinterFunctionsMap {
192211
[index: string]: LinterFunctions;
193212
}
194213

214+
export type LinterFunction = ((...args: any[]) => boolean) | undefined;
215+
export type CompletionFunction = ((...args: any[]) => CompletionItem[]) | undefined;
216+
export type ConditionFunction = ((...args: any[]) => boolean) | undefined;
217+
218+
export interface FunctionItem {
219+
functionName: string;
220+
function: LinterFunction | CompletionFunction | ConditionFunction;
221+
}
222+
195223
export interface LinterFunctions {
196-
[index: string]: (element: Element) => boolean;
224+
[index: string]: LinterFunction;
197225
}
198226

199227
export interface LanguageService {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {
2+
ApidomCompletionItem,
3+
CompletionFormat,
4+
CompletionType,
5+
} from '../../../../apidom-language-types';
6+
7+
const asyncapiVersionComplete: ApidomCompletionItem[] = [
8+
{
9+
label: '2.0.0',
10+
insertText: '2.0.0',
11+
kind: 12,
12+
format: CompletionFormat.QUOTED_FORCED,
13+
type: CompletionType.VALUE,
14+
insertTextFormat: 2,
15+
},
16+
{
17+
label: '2.1.0',
18+
insertText: '2.1.0',
19+
kind: 12,
20+
format: CompletionFormat.QUOTED_FORCED,
21+
type: CompletionType.VALUE,
22+
insertTextFormat: 2,
23+
},
24+
{
25+
label: '2.2.0',
26+
insertText: '2.2.0',
27+
kind: 12,
28+
format: CompletionFormat.QUOTED_FORCED,
29+
type: CompletionType.VALUE,
30+
insertTextFormat: 2,
31+
},
32+
];
33+
34+
export default asyncapiVersionComplete;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import ApilintCodes from '../../../codes';
2+
import { LinterMeta } from '../../../../apidom-language-types';
3+
4+
const asyncapiVersionLint20: LinterMeta = {
5+
code: ApilintCodes.ASYNCAPI_ASYNCAPIVERSION_20,
6+
source: 'apilint',
7+
message: "'asyncapi' value must be 2.0.0",
8+
severity: 1,
9+
linterFunction: 'apilintValueRegex',
10+
linterParams: ['2\\.0\\.0'],
11+
marker: 'value',
12+
targetSpecs: [{ namespace: 'asyncapi', version: '2.0.0' }],
13+
data: {
14+
quickFix: [
15+
{
16+
message: "update to '2.0.0'",
17+
action: 'updateValue',
18+
functionParams: ['2.0.0'],
19+
},
20+
],
21+
},
22+
};
23+
24+
export default asyncapiVersionLint20;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import ApilintCodes from '../../../codes';
2+
import { LinterMeta } from '../../../../apidom-language-types';
3+
4+
const asyncapiVersionLint21: LinterMeta = {
5+
code: ApilintCodes.ASYNCAPI_ASYNCAPIVERSION_21,
6+
source: 'apilint',
7+
message: "'asyncapi' value must be 2.1.0",
8+
severity: 1,
9+
linterFunction: 'apilintValueRegex',
10+
linterParams: ['2\\.1\\.0'],
11+
marker: 'value',
12+
targetSpecs: [{ namespace: 'asyncapi', version: '2.1.0' }],
13+
data: {
14+
quickFix: [
15+
{
16+
message: "update to '2.1.0'",
17+
action: 'updateValue',
18+
functionParams: ['2.1.0'],
19+
},
20+
],
21+
},
22+
};
23+
24+
export default asyncapiVersionLint21;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import ApilintCodes from '../../../codes';
2+
import { LinterMeta } from '../../../../apidom-language-types';
3+
4+
const asyncapiVersionLint22: LinterMeta = {
5+
code: ApilintCodes.ASYNCAPI_ASYNCAPIVERSION_22,
6+
source: 'apilint',
7+
message: "'asyncapi' value must be 2.2.0",
8+
severity: 1,
9+
linterFunction: 'apilintValueRegex',
10+
linterParams: ['2\\.2\\.0'],
11+
marker: 'value',
12+
targetSpecs: [{ namespace: 'asyncapi', version: '2.2.0' }],
13+
data: {
14+
quickFix: [
15+
{
16+
message: "update to '2.2.0'",
17+
action: 'updateValue',
18+
functionParams: ['2.2.0'],
19+
},
20+
],
21+
},
22+
};
23+
24+
export default asyncapiVersionLint22;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import asyncapiVersionLint20 from './asyncapi-20';
2+
import asyncapiVersionLint21 from './asyncapi-21';
3+
import asyncapiVersionLint22 from './asyncapi-22';
4+
5+
const asyncapiVersionLints = [asyncapiVersionLint20, asyncapiVersionLint21, asyncapiVersionLint22];
6+
7+
export default asyncapiVersionLints;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import asyncapiVersionLints from './lint/lints';
2+
// import asyncapiVersionComplete from './complete/asyncapiversion';
3+
import { FormatMeta } from '../../../apidom-language-types';
4+
5+
const asyncApiVersionMeta: FormatMeta = {
6+
lint: asyncapiVersionLints,
7+
// completion: asyncapiVersionComplete,
8+
};
9+
10+
export default asyncApiVersionMeta;

0 commit comments

Comments
 (0)