@spyglassmc/mcdoc 0.3.7 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ import type * as core from '@spyglassmc/core';
2
+ import type { McdocType, StructTypePairField } from '../../type/index.js';
3
+ import type { SimplifiedMcdocType } from '../checker/index.js';
4
+ export type SimpleCompletionField = {
5
+ key: string;
6
+ field: core.DeepReadonly<StructTypePairField>;
7
+ };
8
+ export interface McdocCompleterContext extends core.CompleterContext {
9
+ requireCanonical?: boolean;
10
+ }
11
+ export declare function getFields(typeDef: core.DeepReadonly<SimplifiedMcdocType>, ctx: McdocCompleterContext): SimpleCompletionField[];
12
+ export type SimpleCompletionValue = {
13
+ value: string;
14
+ detail?: string;
15
+ kind?: McdocType['kind'];
16
+ completionKind?: core.CompletionKind;
17
+ insertText?: string;
18
+ };
19
+ export declare function getValues(typeDef: core.DeepReadonly<McdocType>, ctx: McdocCompleterContext): SimpleCompletionValue[];
20
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,123 @@
1
+ import { TypeDefSymbolData } from '../../binder/index.js';
2
+ import { handleAttributes } from '../attribute/index.js';
3
+ export function getFields(typeDef, ctx) {
4
+ switch (typeDef.kind) {
5
+ case 'union':
6
+ const allFields = new Map();
7
+ for (const member of typeDef.members) {
8
+ for (const field of getFields(member, ctx)) {
9
+ allFields.set(field.key, field);
10
+ }
11
+ }
12
+ return [...allFields.values()];
13
+ case 'struct':
14
+ return typeDef.fields.flatMap(field => {
15
+ if (typeof field.key === 'string') {
16
+ return [{ key: field.key, field }];
17
+ }
18
+ if (field.key.kind === 'string'
19
+ || (field.key.kind === 'literal' && field.key.value.kind === 'string')
20
+ || (field.key.kind === 'enum' && field.key.enumKind === 'string')) {
21
+ const ans = getStringCompletions(field.key, ctx);
22
+ if (ans.length > 0) {
23
+ return ans.map(c => ({ key: c.value, field }));
24
+ }
25
+ }
26
+ if (field.key.kind === 'literal') {
27
+ return [{ key: `${field.key.value.value}`, field }];
28
+ }
29
+ if (field.key.kind === 'string') {
30
+ return getStringCompletions(field.key, ctx)
31
+ .map(c => ({ key: c.value, field }));
32
+ }
33
+ return [];
34
+ });
35
+ default:
36
+ return [];
37
+ }
38
+ }
39
+ // TODO: only accept SimplifiedMcdocType here
40
+ export function getValues(typeDef, ctx) {
41
+ if (typeDef.kind === 'string'
42
+ || (typeDef.kind === 'literal' && typeDef.value.kind === 'string')
43
+ || (typeDef.kind === 'enum' && typeDef.enumKind === 'string')) {
44
+ const ans = getStringCompletions(typeDef, ctx);
45
+ if (ans.length > 0) {
46
+ return ans;
47
+ }
48
+ }
49
+ switch (typeDef.kind) {
50
+ case 'union':
51
+ const allValues = new Map();
52
+ for (const member of typeDef.members) {
53
+ for (const value of getValues(member, ctx)) {
54
+ allValues.set(value.value, value);
55
+ }
56
+ }
57
+ return [...allValues.values()];
58
+ case 'reference':
59
+ // TODO: de-duplicate this logic from the runtime simplifier
60
+ if (!typeDef.path) {
61
+ return [];
62
+ }
63
+ const symbol = ctx.symbols.query(ctx.doc, 'mcdoc', typeDef.path);
64
+ const def = symbol.getData(TypeDefSymbolData.is)?.typeDef;
65
+ if (!def) {
66
+ return [];
67
+ }
68
+ if (typeDef.attributes?.length) {
69
+ return getValues({
70
+ ...def,
71
+ attributes: [...typeDef.attributes, ...def.attributes ?? []],
72
+ }, ctx);
73
+ }
74
+ return getValues(def, ctx);
75
+ case 'literal':
76
+ return [{ value: `${typeDef.value.value}`, kind: typeDef.value.kind }];
77
+ case 'boolean':
78
+ return ['false', 'true'].map(v => ({ value: v, kind: 'boolean' }));
79
+ case 'enum':
80
+ // TODO: de-duplicate this logic from the runtime simplifier
81
+ const filteredValues = typeDef.values.filter(value => {
82
+ let keep = true;
83
+ handleAttributes(value.attributes, ctx, (handler, config) => {
84
+ if (!keep || !handler.filterElement) {
85
+ return;
86
+ }
87
+ if (!handler.filterElement(config, ctx)) {
88
+ keep = false;
89
+ }
90
+ });
91
+ return keep;
92
+ });
93
+ return filteredValues.map(v => ({
94
+ value: `${v.value}`,
95
+ detail: v.identifier,
96
+ kind: typeDef.enumKind ?? 'string',
97
+ }));
98
+ default:
99
+ return [];
100
+ }
101
+ }
102
+ function getStringCompletions(typeDef, ctx) {
103
+ const ans = [];
104
+ handleAttributes(typeDef.attributes, ctx, (handler, config) => {
105
+ const mock = handler.stringMocker?.(config, typeDef, ctx);
106
+ if (!mock) {
107
+ return;
108
+ }
109
+ const items = ctx.meta.getCompleter(mock.type)(mock, ctx);
110
+ ans.push(...items.map(item => ({
111
+ value: item.label,
112
+ kind: 'string',
113
+ detail: item.detail,
114
+ completionKind: item.kind,
115
+ insertText: item.insertText,
116
+ })));
117
+ });
118
+ if (ans.length === 0 && typeDef.kind === 'literal') {
119
+ ans.push({ value: `${typeDef.value.value}`, kind: 'string' });
120
+ }
121
+ return ans;
122
+ }
123
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,5 @@
1
+ export { McdocAttribute, registerAttribute } from './attribute/index.js';
2
+ export * as attribute from './attribute/index.js';
3
+ export * as checker from './checker/index.js';
4
+ export * as completer from './completer/index.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,5 @@
1
+ export { registerAttribute } from './attribute/index.js';
2
+ export * as attribute from './attribute/index.js';
3
+ export * as checker from './checker/index.js';
4
+ export * as completer from './completer/index.js';
5
+ //# sourceMappingURL=index.js.map
@@ -1,21 +1,39 @@
1
- import type { FullResourceLocation, ProcessorContext } from '@spyglassmc/core';
2
- import type { EnumKind, RangeKind } from '../node/index.js';
1
+ import type { FullResourceLocation } from '@spyglassmc/core';
2
+ import type { EnumKind } from '../node/index.js';
3
+ import { RangeKind } from '../node/index.js';
4
+ export type Attributes = Attribute[];
5
+ export declare namespace Attributes {
6
+ function equals(a: Attributes | undefined, b: Attributes | undefined): boolean;
7
+ }
3
8
  export interface Attribute {
4
9
  name: string;
5
10
  value?: AttributeValue;
6
11
  }
7
- export type AttributeValue = McdocType | {
12
+ export declare namespace Attribute {
13
+ function equals(a: Attribute, b: Attribute): boolean;
14
+ }
15
+ export type AttributeValue = McdocType | AttributeTreeValue;
16
+ export interface AttributeTreeValue {
8
17
  kind: 'tree';
9
18
  values: AttributeTree;
10
- };
11
- export type AttributeTree = {
19
+ }
20
+ export interface AttributeTree {
12
21
  [key: string | number]: AttributeValue;
13
- };
22
+ }
23
+ export declare namespace AttributeValue {
24
+ function equals(a: AttributeValue, b: AttributeValue): boolean;
25
+ }
14
26
  export type NumericRange = {
15
27
  kind: RangeKind;
16
28
  min?: number;
17
29
  max?: number;
18
30
  };
31
+ export declare namespace NumericRange {
32
+ function isInRange(range: NumericRange, val: number): boolean;
33
+ function equals(a: NumericRange, b: NumericRange): boolean;
34
+ function intersect(a: NumericRange, b: NumericRange): NumericRange;
35
+ function toString({ kind, min, max }: NumericRange): string;
36
+ }
19
37
  export declare const StaticIndexKeywords: readonly ["fallback", "none", "unknown", "spawnitem", "blockitem"];
20
38
  export type StaticIndexKeyword = (typeof StaticIndexKeywords)[number];
21
39
  export interface StaticIndex {
@@ -33,99 +51,101 @@ export type Index = StaticIndex | DynamicIndex;
33
51
  * Corresponds to the IndexBodyNode
34
52
  */
35
53
  export type ParallelIndices = Index[];
54
+ export declare namespace ParallelIndices {
55
+ function equals(a: ParallelIndices, b: ParallelIndices): boolean;
56
+ }
36
57
  export interface DispatcherData {
37
58
  registry: FullResourceLocation;
38
59
  parallelIndices: ParallelIndices;
39
60
  }
40
- export interface DispatcherType extends DispatcherData {
61
+ export interface DispatcherType extends DispatcherData, McdocBaseType {
41
62
  kind: 'dispatcher';
42
63
  }
43
- export interface StructType {
64
+ export interface StructType extends McdocBaseType {
44
65
  kind: 'struct';
45
66
  fields: StructTypeField[];
46
67
  }
47
68
  export type StructTypeField = StructTypePairField | StructTypeSpreadField;
48
- export interface StructTypePairField {
69
+ export interface StructTypePairField extends McdocBaseType {
49
70
  kind: 'pair';
50
- attributes?: Attribute[];
51
71
  key: string | McdocType;
52
72
  type: McdocType;
53
73
  optional?: boolean;
74
+ deprecated?: boolean;
75
+ desc?: string;
54
76
  }
55
- export interface StructTypeSpreadField {
77
+ export interface StructTypeSpreadField extends McdocBaseType {
56
78
  kind: 'spread';
57
- attributes?: Attribute[];
58
79
  type: McdocType;
59
80
  }
60
- export interface EnumType {
81
+ export interface EnumType extends McdocBaseType {
61
82
  kind: 'enum';
62
83
  enumKind?: EnumKind;
63
84
  values: EnumTypeField[];
64
85
  }
65
- export interface EnumTypeField {
66
- attributes?: Attribute[];
86
+ export interface EnumTypeField extends McdocBaseType {
67
87
  identifier: string;
68
- value: string | number | bigint;
88
+ value: string | number;
69
89
  }
70
- export interface ReferenceType {
90
+ export interface ReferenceType extends McdocBaseType {
71
91
  kind: 'reference';
72
92
  path?: string;
73
93
  }
74
- export interface UnionType<T extends McdocType = McdocType> {
94
+ export interface UnionType<T extends McdocType = McdocType> extends McdocBaseType {
75
95
  kind: 'union';
76
96
  members: T[];
77
97
  }
78
- export interface AttributedType {
79
- kind: 'attributed';
80
- attribute: Attribute;
81
- child: McdocType;
82
- }
83
- export interface IndexedType {
98
+ export interface IndexedType extends McdocBaseType {
84
99
  kind: 'indexed';
85
100
  parallelIndices: Index[];
86
101
  child: McdocType;
87
102
  }
88
- export interface TemplateType {
103
+ export interface TemplateType extends McdocBaseType {
89
104
  kind: 'template';
90
105
  child: McdocType;
91
106
  typeParams: {
92
107
  path: string;
93
108
  }[];
94
109
  }
95
- export interface ConcreteType {
110
+ export interface ConcreteType extends McdocBaseType {
96
111
  kind: 'concrete';
97
112
  child: McdocType;
98
113
  typeArgs: McdocType[];
99
114
  }
100
- export declare const EmptyUnion: UnionType<never> & NoIndices;
101
- export declare function createEmptyUnion(attributes?: Attribute[]): UnionType<never> & NoIndices;
102
- export interface KeywordType {
115
+ export interface MappedType extends McdocBaseType {
116
+ kind: 'mapped';
117
+ child: McdocType;
118
+ mapping: {
119
+ [path: string]: McdocType;
120
+ };
121
+ }
122
+ export declare const EmptyUnion: UnionType<never>;
123
+ export declare function createEmptyUnion(attributes?: Attributes): UnionType<never>;
124
+ export interface KeywordType extends McdocBaseType {
103
125
  kind: 'any' | 'boolean' | 'unsafe';
104
126
  }
105
- export interface StringType {
127
+ export interface StringType extends McdocBaseType {
106
128
  kind: 'string';
107
129
  lengthRange?: NumericRange;
108
130
  }
109
- export type LiteralValue = {
131
+ export type LiteralValue = LiteralBooleanValue | LiteralStringValue | LiteralNumericValue;
132
+ export interface LiteralBooleanValue {
110
133
  kind: 'boolean';
111
134
  value: boolean;
112
- } | {
135
+ }
136
+ export interface LiteralStringValue {
113
137
  kind: 'string';
114
138
  value: string;
115
- } | {
116
- kind: 'number';
139
+ }
140
+ export interface LiteralNumericValue {
141
+ kind: NumericTypeKind;
117
142
  value: number;
118
- suffix: 'b' | 's' | 'l' | 'f' | 'd' | undefined;
119
- };
120
- export interface LiteralType {
143
+ }
144
+ export interface LiteralType extends McdocBaseType {
121
145
  kind: 'literal';
122
146
  value: LiteralValue;
123
147
  }
124
- export declare const LiteralNumberSuffixes: readonly ["b", "s", "l", "f", "d"];
125
- export type LiteralNumberSuffix = (typeof LiteralNumberSuffixes)[number];
126
- export declare const LiteralNumberCaseInsensitiveSuffixes: readonly ["b", "s", "l", "f", "d", "B", "S", "L", "F", "D"];
127
- export type LiteralNumberCaseInsensitiveSuffix = (typeof LiteralNumberCaseInsensitiveSuffixes)[number];
128
- export interface NumericType {
148
+ export interface NumericType extends McdocBaseType {
129
149
  kind: NumericTypeKind;
130
150
  valueRange?: NumericRange;
131
151
  }
@@ -135,7 +155,7 @@ export declare const NumericTypeFloatKinds: readonly ["float", "double"];
135
155
  export type NumericTypeFloatKind = (typeof NumericTypeFloatKinds)[number];
136
156
  export declare const NumericTypeKinds: readonly ["byte", "short", "int", "long", "float", "double"];
137
157
  export type NumericTypeKind = (typeof NumericTypeKinds)[number];
138
- export interface PrimitiveArrayType {
158
+ export interface PrimitiveArrayType extends McdocBaseType {
139
159
  kind: 'byte_array' | 'int_array' | 'long_array';
140
160
  valueRange?: NumericRange;
141
161
  lengthRange?: NumericRange;
@@ -144,63 +164,24 @@ export declare const PrimitiveArrayValueKinds: readonly ["byte", "int", "long"];
144
164
  export type PrimitiveArrayValueKind = (typeof PrimitiveArrayValueKinds)[number];
145
165
  export declare const PrimitiveArrayKinds: readonly ("byte_array" | "int_array" | "long_array")[];
146
166
  export type PrimitiveArrayKind = (typeof PrimitiveArrayKinds)[number];
147
- export interface ListType {
167
+ export interface ListType extends McdocBaseType {
148
168
  kind: 'list';
149
169
  item: McdocType;
150
170
  lengthRange?: NumericRange;
151
171
  }
152
- export interface TupleType {
172
+ export interface TupleType extends McdocBaseType {
153
173
  kind: 'tuple';
154
174
  items: McdocType[];
155
175
  }
156
- export type McdocType = DispatcherType | EnumType | KeywordType | ListType | LiteralType | NumericType | PrimitiveArrayType | ReferenceType | StringType | StructType | TupleType | UnionType | AttributedType | IndexedType | TemplateType | ConcreteType;
176
+ export interface McdocBaseType {
177
+ attributes?: Attributes;
178
+ }
179
+ export type McdocType = DispatcherType | EnumType | KeywordType | ListType | LiteralType | NumericType | PrimitiveArrayType | ReferenceType | StringType | StructType | TupleType | UnionType | IndexedType | TemplateType | ConcreteType | MappedType;
157
180
  export declare namespace McdocType {
181
+ function equals(a: McdocType, b: McdocType): boolean;
158
182
  function toString(type: McdocType | undefined): string;
159
183
  }
160
- /**
161
- * A type that doesn't include a dispatcher type.
162
- */
163
- export type DispatchedType = Exclude<McdocType, DispatcherType | UnionType> | UnionType<DispatchedType>;
164
- /**
165
- * A type that doesn't include a reference type.
166
- */
167
- export type DereferencedType = Exclude<McdocType, ReferenceType | UnionType> | UnionType<DereferencedType>;
168
- /**
169
- * A type that doesn't include a dispatcher type or a reference type.
170
- */
171
- export type TangibleType = Exclude<McdocType, DispatcherType | ReferenceType | UnionType> | UnionType<TangibleType>;
172
- /**
173
- * A type that is {@link TangibleType} and doesn't have any indices.
174
- */
175
- export type ResolvedType = (Exclude<McdocType, DispatcherType | ReferenceType | UnionType> | UnionType<ResolvedType>) & NoIndices;
176
- type NoIndices = {
177
- indices?: undefined;
178
- };
179
- export interface FlatStructType {
180
- kind: 'flat_struct';
181
- fields: Record<string, McdocType>;
182
- }
183
- export declare const flattenUnionType: (union: UnionType) => UnionType;
184
- export declare const unionTypes: (a: McdocType, b: McdocType) => McdocType;
185
- export declare const simplifyUnionType: (union: UnionType) => McdocType;
186
- export declare const simplifyListType: (list: ListType) => ListType;
187
- export declare const simplifyType: (data: McdocType) => McdocType;
188
- export declare const checkAssignability: ({ source, target, }: {
189
- source: McdocType | undefined;
190
- target: McdocType | undefined;
191
- }) => {
192
- isAssignable: boolean;
193
- errorMessage?: string;
194
- };
195
- /**
196
- * https://spyglassmc.com/user/mcdoc/#p-RuntimeValue
197
- */
198
- export interface RuntimeValue {
199
- asString(): string | undefined;
200
- getKeyOnParent(): RuntimeValue | undefined;
201
- getParent(): RuntimeValue | undefined;
202
- getValue(key: string): RuntimeValue | undefined;
203
- }
204
- export declare function resolveType(inputType: McdocType, ctx: ProcessorContext, value: RuntimeValue | undefined): ResolvedType;
205
- export {};
184
+ export interface UseStatementBindingData {
185
+ target: readonly string[];
186
+ }
206
187
  //# sourceMappingURL=index.d.ts.map