This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDoCheckDocumentation.ts
157 lines (135 loc) · 7.89 KB
/
DoCheckDocumentation.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { window, workspace, TextEditor, languages, commands, Position, SnippetString, TextDocument, Range } from "vscode";
import { ALCheckDocumentation } from "./util/ALCheckDocumentation";
import { ALFixDocumentation } from "./util/ALFixDocumentation";
import { VSCodeApi } from "./api/VSCodeApi";
import { ALDocCommentUtil } from "./util/ALDocCommentUtil";
import { ALSyntaxUtil } from "./util/ALSyntaxUtil";
export class DoCheckDocumentation {
private activeEditor!: TextEditor;
private alUpdateDecorations: ALCheckDocumentation = new ALCheckDocumentation();
constructor() {
window.onDidChangeActiveTextEditor(editor => {
if ((editor === undefined) || (editor === null)) {
return;
}
this.activeEditor = editor;
this.alUpdateDecorations.CheckDocumentation(this.activeEditor.document);
});
workspace.onDidChangeTextDocument(event => {
if (!this.activeEditor) {
if (window.activeTextEditor?.document === event.document) {
this.activeEditor = window.activeTextEditor;
}
}
if ((!this.activeEditor) || (event.document !== this.activeEditor.document)) {
return;
}
this.alUpdateDecorations.CheckDocumentation(this.activeEditor.document);
});
languages.registerCodeActionsProvider('al',
new ALFixDocumentation(), {
providedCodeActionKinds: ALFixDocumentation.providedCodeActionKinds
});
// quick fix commands
commands.registerCommand("bdev-al-xml-doc.fixDocumentation", (procedureState: { name: string; position: Range; definition: { [key: string]: string; }; documentation: string } | null) => {
this.FixDocumentation(window.activeTextEditor, procedureState);
});
commands.registerCommand("bdev-al-xml-doc.fixSummaryDocumentation", (procedureState: { name: string; position: Range; definition: { [key: string]: string; }; documentation: string } | null) => {
this.FixSummaryDocumentation(window.activeTextEditor, procedureState);
});
commands.registerCommand("bdev-al-xml-doc.fixParameterDocumentation", (procedureState: { name: string; position: Range; definition: { [key: string]: string; }; documentation: string } | null) => {
this.FixParameterDocumentation(window.activeTextEditor, procedureState);
});
commands.registerCommand("bdev-al-xml-doc.fixReturnTypeDocumentation", (procedureState: { name: string; position: Range; definition: { [key: string]: string; }; documentation: string } | null) => {
this.FixReturnTypeDocumentation(window.activeTextEditor, procedureState);
});
this.InitializeCheckDocumentation();
}
private InitializeCheckDocumentation() {
workspace.findFiles('**/*.al').then(allFiles => {
allFiles.forEach(file => {
workspace.openTextDocument(file.fsPath).then(document => {
this.alUpdateDecorations.CheckDocumentation(document);
});
});
});
}
private FixDocumentation(editor: TextEditor | undefined, procedureState: { name: string; position: Range; definition: { [key: string]: string; }; documentation: string } | null) {
if (!editor) {
return;
}
editor.insertSnippet(new SnippetString(
ALDocCommentUtil.GenerateProcedureDocString(
ALSyntaxUtil.AnalyzeProcedureDefinition(editor.document.getText().split("\r\n")[procedureState!.position.start.line])!.groups!) + "\n"),
new Position(procedureState!.position.start.line, ALDocCommentUtil.GetLineStartPosition(editor.document, procedureState!.position.start.line))); //vsCodeApi.GetActiveLineStartPosition());
}
private FixSummaryDocumentation(editor: TextEditor | undefined, procedureState: { name: string; position: Range; definition: { [key: string]: string; }; documentation: string } | null) {
if (!editor) {
return;
}
try {
let procedureDocumentation = ALDocCommentUtil.GenerateProcedureDocString(ALSyntaxUtil.AnalyzeProcedureDefinition(editor.document.getText().split("\r\n")[procedureState!.position.start.line])!.groups!);
let lineNo = ALDocCommentUtil.GetFirstXmlDocumentationLineNo(editor, procedureState!.position.start.line);
if (lineNo === -1) {
return;
}
editor.insertSnippet(new SnippetString(ALDocCommentUtil.GetXmlDocumentationNode(procedureDocumentation, 'summary') + "\n"),
new Position(lineNo, ALDocCommentUtil.GetLineStartPosition(editor.document, lineNo)));
} catch {
return;
}
}
private FixParameterDocumentation(editor: TextEditor | undefined, procedureState: { name: string; position: Range; definition: { [key: string]: string; }; documentation: string } | null) {
if (!editor) {
return;
}
try {
let procedureDefinition = ALSyntaxUtil.AnalyzeProcedureDefinition(editor.document.getText().split("\r\n")[procedureState!.position.start.line])!.groups!;
let procedureDocumentation = ALDocCommentUtil.GenerateProcedureDocString(procedureDefinition);
let jsonDocumentation = ALDocCommentUtil.GetJsonFromXmlDocumentation(procedureDocumentation);
let parameters: { name: string; documentation: string; insertAtLineNo: number }[] = [];
jsonDocumentation.param.forEach((param: { attr: { name: string; }; value: string; }) => {
parameters.push({
name: param.attr.name,
documentation: param.value,
insertAtLineNo: ALDocCommentUtil.GetXmlDocumentationNodeLineNo(editor, procedureState!.position.start.line, 'param', 'name', param.attr.name)
});
});
let i = 0;
let j = 0;
parameters.forEach(parameter => {
if (parameter.insertAtLineNo === -1) {
if ((i === 0) || (parameters[i-1].insertAtLineNo === -1)) {
parameter.insertAtLineNo = ALDocCommentUtil.GetXmlDocumentationNodeLineNo(editor, procedureState!.position.start.line, 'summary');
} else {
parameter.insertAtLineNo = parameters[i-1].insertAtLineNo + j;
}
editor.insertSnippet(new SnippetString(ALDocCommentUtil.GetXmlDocumentationNode(procedureDocumentation, 'param', 'name', parameter.name) + "\n"),
new Position(parameter.insertAtLineNo, ALDocCommentUtil.GetLineStartPosition(editor.document, parameter.insertAtLineNo)));
j++;
}
i++;
});
} catch {
return;
}
}
private FixReturnTypeDocumentation(editor: TextEditor | undefined, procedureState: { name: string; position: Range; definition: { [key: string]: string; }; documentation: string } | null) {
if (!editor) {
return;
}
try {
let procedureDocumentation = ALDocCommentUtil.GenerateProcedureDocString(ALSyntaxUtil.AnalyzeProcedureDefinition(editor.document.getText().split("\r\n")[procedureState!.position.start.line])!.groups!);
let lineNo = ALDocCommentUtil.GetLastXmlDocumentationLineNo(editor, procedureState!.position.start.line);
if (lineNo === -1) {
return;
}
editor.insertSnippet(new SnippetString(ALDocCommentUtil.GetXmlDocumentationNode(procedureDocumentation, 'returns') + "\n"),
new Position(lineNo, ALDocCommentUtil.GetLineStartPosition(editor.document, lineNo)));
} catch {
return;
}
}
public dispose() {
}
}