Skip to content

Commit f60821b

Browse files
committed
Added documentation for types
1 parent a2a073b commit f60821b

File tree

3 files changed

+117
-18
lines changed

3 files changed

+117
-18
lines changed

server/src/ast.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ export class AST {
136136
if (specifier.imported.type === "Identifier") {
137137
const real_name = specifier.imported.name;
138138
const name = specifier.local.name;
139-
let imported: Documentation | undefined;
140-
if ((imported = getImportedName(module_name, real_name)) !== undefined) {
139+
let imported: Documentation | undefined = getImportedName(module_name, real_name);
140+
if (imported !== undefined) {
141141
this.imported_names[module_name].set(real_name, { range: sourceLocToRange(specifier.loc!), local: name });
142142
const declaration: ImportedSymbol = {
143143
name: name,
@@ -474,6 +474,7 @@ export class AST {
474474
data: { type: AUTOCOMPLETE_TYPES.SYMBOL, ...item.data }
475475
};
476476
}
477+
// Check if last import specifier exists after prepend, if so allow insert there
477478
else if ([...map][map.size-1][1].range.start.line >= this.prependLines){
478479
return {
479480
...item,
@@ -483,6 +484,7 @@ export class AST {
483484
};
484485
};
485486
}
487+
// Default case, insert at the first line after prepend
486488
return {
487489
...item,
488490
additionalTextEdits: [

server/src/types.ts

Lines changed: 111 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Range } from "vscode-languageserver/node";
22
import * as es from 'estree';
33

4-
// Note that the order the enum fields appear in determine the order they are displayed in the autocomplete list
4+
/**
5+
* Enum for differentiating between types of autocomplete, and also used for sorting the autocomplete items within the autocomplete list
6+
*/
57
export enum AUTOCOMPLETE_TYPES {
6-
BUILTIN,
8+
BUILTIN,
79
KEYWORD,
8-
SYMBOL,
9-
MODULE
10+
SYMBOL,
11+
MODULE
1012
}
1113

1214
export enum NODES {
@@ -46,55 +48,141 @@ export enum EXPRESSIONS {
4648
LAMBDA = "ArrowFunctionExpression"
4749
}
4850

51+
/**
52+
* Stores information about how that symbol was declared
53+
*/
4954
export interface DeclarationSymbol {
55+
/**
56+
* Name of the symbol, taken from the identifier
57+
*/
5058
name: string,
59+
/**
60+
* Scope where the symbol can be used
61+
* Defined as a SourceLocation object, which has 1-index lines
62+
*/
5163
scope: es.SourceLocation,
64+
/**
65+
* Meta information about the declaration
66+
* If a constant is declared with a lambda, it will be assigned func
67+
* If a let is declared with a lambda, it will still be assigned let as it can be reassigned later on
68+
*/
5269
meta: "const" | "let" | "func",
70+
/**
71+
* More meta information about the declaration
72+
* Has some overlap with `meta`, could be combined in the future
73+
*/
5374
declarationKind: DeclarationKind,
75+
/**
76+
* Range used only for highlighting purposes in DocumentSymbols, not really used anywhere else
77+
*/
5478
range: Range,
79+
/**
80+
* Range representing the location for the identifier of this symbol.
81+
*/
5582
selectionRange: Range,
83+
/**
84+
* Used for functions and lambdas to process child symbols in DocumentSymbols
85+
*/
5686
parameters?: Array<ParameterSymbol>,
87+
/**
88+
* Flag whether to show this symbol in DocumentSymbols
89+
* As of now, only lambda and function params have this flag set to true, as they are already in the children of the function symbol
90+
*/
5791
showInDocumentSymbols?: boolean;
92+
/**
93+
* Flags whether this symbol has been used or not.
94+
* Default should be set to true, then when an identifier for this declaration is found, flag can be set to false
95+
* Right now this check occurs in the identifierRule when parsing for error diagnostics
96+
*/
5897
unused: boolean
5998
}
6099

61100
export interface ParameterSymbol extends DeclarationSymbol {
101+
/**
102+
* Flag for whether this symbol is a rest element or not. Used for some diagnostics.
103+
*/
62104
isRestElement: boolean
63105
}
64106

65107
export interface ImportedSymbol extends DeclarationSymbol {
108+
/**
109+
* String representing the module name where this symbol was imported from
110+
*/
66111
module_name: string,
112+
/**
113+
* String representing the actual defined name of this symbol in the module
114+
* This should be ImportSpecifier.imported.name
115+
* Not to be confused with ImportedSpecifier.local.name, which is the name for the DocumentSymbol this inherits from
116+
*/
67117
real_name: string
68118
}
69119

120+
/**
121+
* Represents documentation scraped from Source Academy docs
122+
*/
70123
export interface Documentation {
124+
/**
125+
* The name of the function/constant
126+
*/
71127
label: string,
128+
/**
129+
* Title of the description for the documentation
130+
*/
72131
title: string,
132+
/**
133+
* The whole description of this documentation
134+
*/
73135
description: string,
136+
/**
137+
* Meta information whether this is a constant or a function
138+
*/
74139
meta: "const" | "func",
140+
/**
141+
* String array of parameters if this is a function
142+
*/
75143
parameters?: string[],
144+
/**
145+
* Strings for names of optional parameters
146+
*/
76147
optional_params?: string[],
77-
hasRestElement? : boolean
148+
/**
149+
* Flag whether the last parameter is a rest element
150+
* If so the function can take in any amount of parameters after the last one
151+
*/
152+
hasRestElement?: boolean
78153
}
79154

155+
/**
156+
* Interface for data to be stored within CompletionItems
157+
*/
80158
export interface CompletionItemData {
159+
/**
160+
* Type of autocomplete
161+
*/
81162
type: AUTOCOMPLETE_TYPES,
82-
idx: number
163+
/**
164+
* Module name if AutoComplete comes from a Module, used for generating auto imports
165+
*/
83166
module_name?: string,
167+
/**
168+
* String array if autocomplete is a function, used for generating autocomplete parameter snippets
169+
*/
84170
parameters?: string[],
85-
optional_params?: string[],
86-
hasRestElement?: boolean
171+
/**
172+
* String array of optional parameters, used to ignore parameters in autocomplete snippet
173+
*/
174+
optional_params?: string[]
87175
}
88176

89177

90178
// Taken from js-slang
91179
export enum DeclarationKind {
92-
KIND_IMPORT = 'import',
93-
KIND_FUNCTION = 'func',
94-
KIND_LET = 'let',
95-
KIND_PARAM = 'param',
96-
KIND_CONST = 'const',
97-
KIND_KEYWORD = 'keyword'
180+
KIND_IMPORT = 'import',
181+
KIND_FUNCTION = 'func',
182+
KIND_LET = 'let',
183+
KIND_PARAM = 'param',
184+
KIND_CONST = 'const',
185+
KIND_KEYWORD = 'keyword'
98186
}
99187

100188
export enum Chapter {
@@ -104,7 +192,16 @@ export enum Chapter {
104192
SOURCE_4 = 4,
105193
}
106194

195+
/**
196+
* Context for the file being edited
197+
*/
107198
export interface Context {
199+
/**
200+
* Source chapter for this file
201+
*/
108202
chapter: Chapter,
203+
/**
204+
* Number of lines of text being prepended to the file
205+
*/
109206
prepend?: number
110207
}

server/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const autocomplete_labels = source.map(version => version.map((doc, idx):
1818
value: doc.description
1919
},
2020
kind: doc.meta === "const" ? CompletionItemKind.Constant : CompletionItemKind.Function,
21-
data: { type: AUTOCOMPLETE_TYPES.BUILTIN, idx: idx, parameters: doc.parameters, optional_params: doc.optional_params, hasRestElement: doc.hasRestElement } as CompletionItemData,
21+
data: { type: AUTOCOMPLETE_TYPES.BUILTIN, parameters: doc.parameters, optional_params: doc.optional_params } as CompletionItemData,
2222
sortText: '' + AUTOCOMPLETE_TYPES.BUILTIN
2323
};
2424
}));
@@ -39,7 +39,7 @@ for (const key in modules) {
3939
},
4040
kind: doc.meta === "const" ? CompletionItemKind.Constant : CompletionItemKind.Function,
4141
// @ts-ignore
42-
data: { type: AUTOCOMPLETE_TYPES.MODULE, idx: idx, module_name: key, parameters: doc.parameters, optional_params: doc.optional_params } as CompletionItemData,
42+
data: { type: AUTOCOMPLETE_TYPES.MODULE, module_name: key, parameters: doc.parameters, optional_params: doc.optional_params } as CompletionItemData,
4343
sortText: '' + AUTOCOMPLETE_TYPES.MODULE
4444
});
4545
});

0 commit comments

Comments
 (0)