-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
debug-console-session.ts
175 lines (153 loc) · 7.76 KB
/
debug-console-session.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import throttle = require('lodash.throttle');
import { injectable, inject, postConstruct } from 'inversify';
import { DebugProtocol } from 'vscode-debugprotocol/lib/debugProtocol';
import { ConsoleSession, ConsoleItem } from '@theia/console/lib/browser/console-session';
import { AnsiConsoleItem } from '@theia/console/lib/browser/ansi-console-item';
import { DebugSession } from '../debug-session';
import { DebugSessionManager } from '../debug-session-manager';
import { Languages, CompletionItem, CompletionItemKind, Position, Range, TextEdit, Workspace, TextDocument, CompletionParams } from '@theia/languages/lib/browser';
import URI from '@theia/core/lib/common/uri';
import { ExpressionContainer, ExpressionItem } from './debug-console-items';
import { Severity } from '@theia/core/lib/common/severity';
@injectable()
export class DebugConsoleSession extends ConsoleSession {
static uri = new URI().withScheme('debugconsole');
readonly id = 'debug';
protected items: ConsoleItem[] = [];
// content buffer for [append](#append) method
protected uncompletedItemContent: string | undefined;
@inject(DebugSessionManager)
protected readonly manager: DebugSessionManager;
@inject(Languages)
protected readonly languages: Languages;
@inject(Workspace)
protected readonly workspace: Workspace;
protected readonly completionKinds = new Map<DebugProtocol.CompletionItemType | undefined, CompletionItemKind>();
@postConstruct()
init(): void {
this.toDispose.push(this.manager.onDidCreateDebugSession(session => {
if (this.manager.sessions.length === 1) {
this.clear();
}
session.on('output', event => this.logOutput(session, event));
}));
this.completionKinds.set('method', CompletionItemKind.Method);
this.completionKinds.set('function', CompletionItemKind.Function);
this.completionKinds.set('constructor', CompletionItemKind.Constructor);
this.completionKinds.set('field', CompletionItemKind.Field);
this.completionKinds.set('variable', CompletionItemKind.Variable);
this.completionKinds.set('class', CompletionItemKind.Class);
this.completionKinds.set('interface', CompletionItemKind.Interface);
this.completionKinds.set('module', CompletionItemKind.Module);
this.completionKinds.set('property', CompletionItemKind.Property);
this.completionKinds.set('unit', CompletionItemKind.Unit);
this.completionKinds.set('value', CompletionItemKind.Value);
this.completionKinds.set('enum', CompletionItemKind.Enum);
this.completionKinds.set('keyword', CompletionItemKind.Keyword);
this.completionKinds.set('snippet', CompletionItemKind.Snippet);
this.completionKinds.set('text', CompletionItemKind.Text);
this.completionKinds.set('color', CompletionItemKind.Color);
this.completionKinds.set('file', CompletionItemKind.File);
this.completionKinds.set('reference', CompletionItemKind.Reference);
this.completionKinds.set('customcolor', CompletionItemKind.Color);
if (this.languages.registerCompletionItemProvider) {
this.toDispose.push(this.languages.registerCompletionItemProvider([DebugConsoleSession.uri], {
provideCompletionItems: params => this.completions(params)
}, '.'));
}
}
getElements(): IterableIterator<ConsoleItem> {
return this.items.filter(e => !this.severity || e.severity === this.severity)[Symbol.iterator]();
}
protected async completions({ textDocument: { uri }, position }: CompletionParams): Promise<CompletionItem[]> {
const session = this.manager.currentSession;
if (session && session.capabilities.supportsCompletionsRequest) {
const model = monaco.editor.getModel(monaco.Uri.parse(uri));
if (model) {
const column = position.character + 1;
const lineNumber = position.line + 1;
const word = model.getWordAtPosition({ column, lineNumber });
const prefixLength = word ? word.word.length : 0;
const text = model.getValue();
const document = TextDocument.create(uri, model.getModeId(), model.getVersionId(), text);
const items = await session.completions(text, column, lineNumber);
return items.map(item => this.asCompletionItem(document, position, prefixLength, item));
}
}
return [];
}
protected asCompletionItem(document: TextDocument, position: Position, prefixLength: number, item: DebugProtocol.CompletionItem): CompletionItem {
const { label, text, type, length } = item;
const newText = text || label;
const start = document.positionAt(document.offsetAt(position) - (length || prefixLength));
const replaceRange = Range.create(start, position);
const textEdit = TextEdit.replace(replaceRange, newText);
return {
label,
textEdit,
kind: this.completionKinds.get(type)
};
}
async execute(value: string): Promise<void> {
const expression = new ExpressionItem(value, this.manager.currentSession);
this.items.push(expression);
await expression.evaluate();
this.fireDidChange();
}
clear(): void {
this.items = [];
this.fireDidChange();
}
append(value: string): void {
if (!value) {
return;
}
const lastItem = this.items.slice(-1)[0];
if (lastItem instanceof AnsiConsoleItem && lastItem.content === this.uncompletedItemContent) {
this.items.pop();
this.uncompletedItemContent += value;
} else {
this.uncompletedItemContent = value;
}
this.items.push(new AnsiConsoleItem(this.uncompletedItemContent, Severity.Info));
this.fireDidChange();
}
appendLine(value: string): void {
this.items.push(new AnsiConsoleItem(value, Severity.Info));
this.fireDidChange();
}
protected async logOutput(session: DebugSession, event: DebugProtocol.OutputEvent): Promise<void> {
const body = event.body;
const { category, variablesReference } = body;
if (category === 'telemetry') {
console.debug(`telemetry/${event.body.output}`, event.body.data);
return;
}
const severity = category === 'stderr' ? Severity.Error : event.body.category === 'console' ? Severity.Warning : Severity.Info;
if (variablesReference) {
const items = await new ExpressionContainer({ session, variablesReference }).getElements();
this.items.push(...items);
} else if (typeof body.output === 'string') {
for (const line of body.output.split('\n')) {
this.items.push(new AnsiConsoleItem(line, severity));
}
}
this.fireDidChange();
}
protected fireDidChange = throttle(() => super.fireDidChange(), 50);
}