forked from dart-lang/dart-pad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter_popup.dart
235 lines (207 loc) · 7.04 KB
/
parameter_popup.dart
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library dartpad.parameter_popup;
import 'dart:convert';
import 'dart:html';
import 'dart:math' as math;
import 'context.dart';
import 'dart_pad.dart';
import 'editing/editor.dart';
import 'services/common.dart';
import 'services/dartservices.dart';
class ParameterPopup {
static const Set<int> parKeys = {
KeyCode.COMMA,
KeyCode.NINE,
KeyCode.ZERO,
KeyCode.LEFT,
KeyCode.RIGHT,
KeyCode.UP,
KeyCode.DOWN
};
final Context context;
final Editor editor;
final HtmlEscape sanitizer = const HtmlEscape();
ParameterPopup(this.context, this.editor) {
document.onKeyDown.listen(_handleKeyDown);
document.onKeyUp.listen(_handleKeyUp);
document.onClick.listen((e) => _handleClick());
editor.onMouseDown.listen((e) => _handleClick());
}
bool get parPopupActive => querySelector('.parameter-hints') != null;
void remove() {
document.body!.children.remove(querySelector('.parameter-hints'));
}
void _handleKeyDown(KeyboardEvent e) {
if (e.keyCode == KeyCode.ENTER) {
// TODO: Use the onClose event of the completion event to trigger this
_lookupParameterInfo();
}
}
void _handleKeyUp(KeyboardEvent e) {
if (e.keyCode == KeyCode.ESC ||
context.focusedEditor != 'dart' ||
!editor.hasFocus) {
remove();
} else if (parPopupActive || parKeys.contains(e.keyCode)) {
_lookupParameterInfo();
}
}
void _handleClick() {
if (context.focusedEditor != 'dart' || !editor.hasFocus) {
remove();
} else {
_lookupParameterInfo();
}
}
void _lookupParameterInfo() {
var offset = editor.document.indexFromPos(editor.document.cursor);
final source = editor.document.value;
var parInfo = _parameterInfo(source, offset);
if (parInfo == null) {
remove();
return;
}
final openingParenIndex = parInfo['openingParenIndex']!;
final parameterIndex = parInfo['parameterIndex'];
offset = openingParenIndex - 1;
// We request documentation info of what is before the parenthesis.
final input = SourceRequest()
..source = source
..offset = offset;
dartServices
.document(input)
.timeout(serviceCallTimeout)
.then((DocumentResponse result) {
if (!result.info.containsKey('parameters')) {
remove();
return;
}
final parameterInfo = result.info['parameters']!;
var outputString = '';
if (parameterInfo.isEmpty) {
outputString += '<code><no parameters></code>';
} else if (parameterInfo.length < parameterIndex! + 1) {
outputString += '<code>too many parameters listed</code>';
} else {
outputString += '<code>';
for (var i = 0; i < parameterInfo.length; i++) {
if (i == parameterIndex) {
outputString += '<em>${sanitizer.convert(parameterInfo[i])}</em>';
} else {
outputString +=
'<span>${sanitizer.convert(parameterInfo[i])}</span>';
}
if (i != parameterInfo.length - 1) {
outputString += ', ';
}
}
outputString += '</code>';
}
// Check if cursor is still in parameter position
parInfo = _parameterInfo(editor.document.value,
editor.document.indexFromPos(editor.document.cursor));
if (parInfo == null) {
remove();
return;
}
_showParameterPopup(outputString, offset);
});
}
void _showParameterPopup(String string, int methodOffset) {
final editorDiv = querySelector('#editpanel .CodeMirror') as DivElement;
final lineHeightStr =
editorDiv.getComputedStyle().getPropertyValue('line-height');
final lineHeight =
int.parse(lineHeightStr.substring(0, lineHeightStr.indexOf('px')));
// var charWidth = editorDiv.getComputedStyle().getPropertyValue('letter-spacing');
final charWidth = 8;
final methodPosition = editor.document.posFromIndex(methodOffset);
final cursorCoords = editor.getCursorCoords();
final methodCoords = editor.getCursorCoords(position: methodPosition);
final heightOfMethod = (methodCoords.y - lineHeight - 5).round();
DivElement parameterPopup;
if (parPopupActive) {
final parameterHint = querySelector('.parameter-hint')!;
parameterHint.innerHtml = string;
//update popup position
final newLeft = math
.max(
cursorCoords.x - (parameterHint.text!.length * charWidth / 2), 22)
.round();
parameterPopup = querySelector('.parameter-hints') as DivElement
..style.top = '${heightOfMethod}px';
final oldLeftString = parameterPopup.style.left;
final oldLeft =
int.parse(oldLeftString.substring(0, oldLeftString.indexOf('px')));
if ((newLeft - oldLeft).abs() > 50) {
parameterPopup.style.left = '${newLeft}px';
}
} else {
final parameterHint = SpanElement()
..innerHtml = string
..classes.add('parameter-hint');
final left = math
.max(
cursorCoords.x - (parameterHint.text!.length * charWidth / 2), 22)
.round();
parameterPopup = DivElement()
..classes.add('parameter-hints')
..style.left = '${left}px'
..style.top = '${heightOfMethod}px'
..style.maxWidth =
"${querySelector("#editpanel")!.getBoundingClientRect().width}px";
parameterPopup.append(parameterHint);
document.body!.append(parameterPopup);
}
final activeParameter = querySelector('.parameter-hints em');
if (activeParameter != null &&
activeParameter.previousElementSibling != null) {
parameterPopup.scrollLeft =
activeParameter.previousElementSibling!.offsetLeft;
}
}
/// Returns null if the offset is not contained in parenthesis.
/// Otherwise it will return information about the parameters.
/// For example, if the source is `substring(1, <caret>)`, it will return
/// `{openingParenIndex: 9, parameterIndex: 1}`.
Map<String, int>? _parameterInfo(String source, int offset) {
var parameterIndex = 0;
int? openingParenIndex;
var nesting = 0;
while (openingParenIndex == null && offset > 0) {
offset += -1;
if (nesting == 0) {
switch (source[offset]) {
case '(':
openingParenIndex = offset;
break;
case ',':
parameterIndex += 1;
break;
case ';':
return null;
case ')':
nesting += 1;
break;
}
} else {
switch (source[offset]) {
case '(':
nesting += -1;
break;
case ')':
nesting += 1;
break;
}
}
}
return openingParenIndex == null
? null
: {
'openingParenIndex': openingParenIndex,
'parameterIndex': parameterIndex
};
}
}