forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplayString.ts
More file actions
142 lines (114 loc) · 4.81 KB
/
Copy pathdisplayString.ts
File metadata and controls
142 lines (114 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
namespace ts.pxtc.service {
/**
* Produces a markdown string for the symbol that is suitable for display in Monaco
*/
export function displayStringForSymbol(sym: SymbolInfo, python: boolean, apiInfo: ApisInfo) {
if (!sym) return undefined;
switch (sym.kind) {
case SymbolKind.Function:
case SymbolKind.Method:
return displayStringForFunction(sym, python, apiInfo);
case SymbolKind.Enum:
case SymbolKind.EnumMember:
return displayStringForEnum(sym, python);
case SymbolKind.Module:
return displayStringForNamepsace(sym, python);
case SymbolKind.Interface:
return displayStringForInterface(sym, python);
case SymbolKind.Class:
return displayStringForClass(sym, python);
case SymbolKind.Variable:
return displayStringForVariable(sym, python, apiInfo);
case SymbolKind.Property:
return displayStringForProperty(sym, python, apiInfo);
}
return `**${sym.qName}**`
}
export function displayStringForKeyword(keyword: string, python: boolean) {
return `\`\`\`${python ? "py" : "ts"}\n(keyword) ${keyword}\n\`\`\``
}
function displayStringForFunction(sym: SymbolInfo, python: boolean, apiInfo: ApisInfo) {
let prefix = "";
if (sym.kind === SymbolKind.Function) {
prefix += python ? "def " : "function ";
}
else {
prefix += "(method) "
}
prefix += python ? sym.pyQName : sym.qName;
let argString = "";
if (sym.parameters && sym.parameters.length) {
argString = sym.parameters.map(param =>
`${param.name}: ${python ? param.pyTypeString : param.type}`
).join(", ");
}
let retType = sym.retType || "void";
if (python) {
retType = getPythonReturnType(retType, apiInfo);
}
return codeBlock(`${prefix}(${argString}): ${retType}`, python);
}
function displayStringForEnum(sym: SymbolInfo, python: boolean) {
const qName = python ? sym.pyQName : sym.qName
if (sym.kind === SymbolKind.Enum) {
return codeBlock(`enum ${qName}`, python);
}
let memberString = `(enum member) ${qName}`;
if (sym.attributes.enumval) {
memberString += ` = ${sym.attributes.enumval}`;
}
return codeBlock(memberString, false)
}
function displayStringForNamepsace(sym: SymbolInfo, python: boolean) {
return codeBlock(`namespace ${python ? sym.pyQName : sym.qName}`, false);
}
function displayStringForInterface(sym: SymbolInfo, python: boolean) {
return codeBlock(`interface ${python ? sym.pyQName : sym.qName}`, false);
}
function displayStringForClass(sym: SymbolInfo, python: boolean) {
return codeBlock(`class ${python ? sym.pyQName : sym.qName}`, python);
}
function displayStringForVariable(sym: SymbolInfo, python: boolean, apiInfo: ApisInfo) {
let varString = python ? sym.pyQName : `let ${sym.qName}`;
if (sym.retType) {
let retType = sym.retType;
if (python) {
retType = getPythonReturnType(retType, apiInfo);
}
return codeBlock(`${varString}: ${retType}`, python);
}
return codeBlock(varString, python);
}
function displayStringForProperty(sym: SymbolInfo, python: boolean, apiInfo: ApisInfo) {
const propString = `(property) ${python ? sym.pyQName : sym.qName}`;
if (sym.retType) {
let retType = sym.retType;
if (python) {
retType = getPythonReturnType(retType, apiInfo);
}
return codeBlock(`${propString}: ${retType}`, false);
}
return codeBlock(propString, false);
}
function getPythonReturnType(type: string, apiInfo: ApisInfo): string {
switch (type) {
case "void": return "None";
case "boolean": return "bool";
case "string": return "str";
}
if (apiInfo.byQName[type]?.pyQName) {
return apiInfo.byQName[type].pyQName;
}
const arrayMatch = /^(?:Array<(.+)>)|(?:(.+)\[\])|(?:\[.+\])$/.exec(type);
if (arrayMatch) {
return `List[${getPythonReturnType(arrayMatch[1] || arrayMatch[2], apiInfo)}]`;
}
return type;
}
function codeBlock(content: string, python: boolean) {
// The stock TypeScript language service always uses js tags instead of ts. We
// don't include the js language service in monaco, so use ts instead. It produces
// slightly different syntax highlighting
return `\`\`\`${python ? "python" : "ts"}\n${content}\n\`\`\``
}
}