forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.tsx
More file actions
246 lines (217 loc) · 9.23 KB
/
code.tsx
File metadata and controls
246 lines (217 loc) · 9.23 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
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
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';
import * as React from 'react';
import { getLocString } from '../react-common/locReactSide';
import { MonacoEditor } from '../react-common/monacoEditor';
import { InputHistory } from './inputHistory';
import './code.css';
export interface ICodeProps {
autoFocus: boolean;
code : string;
codeTheme: string;
testMode: boolean;
readOnly: boolean;
history: InputHistory | undefined;
showWatermark: boolean;
monacoTheme: string | undefined;
outermostParentClass: string;
editorOptions: monacoEditor.editor.IEditorOptions;
forceBackgroundColor?: string;
onSubmit(code: string): void;
onCreated(code: string, modelId: string): void;
onChange(changes: monacoEditor.editor.IModelContentChange[], modelId: string): void;
openLink(uri: monacoEditor.Uri): void;
}
interface ICodeState {
focused: boolean;
cursorLeft: number;
cursorTop: number;
cursorBottom: number;
charUnderCursor: string;
allowWatermark: boolean;
editor: monacoEditor.editor.IStandaloneCodeEditor | undefined;
model: monacoEditor.editor.ITextModel | null;
}
export class Code extends React.Component<ICodeProps, ICodeState> {
private subscriptions: monacoEditor.IDisposable[] = [];
private lastCleanVersionId: number = 0;
private editorRef: React.RefObject<MonacoEditor> = React.createRef<MonacoEditor>();
constructor(prop: ICodeProps) {
super(prop);
this.state = {focused: false, cursorLeft: 0, cursorTop: 0, cursorBottom: 0, charUnderCursor: '', allowWatermark: true, editor: undefined, model: null};
}
public componentWillUnmount = () => {
this.subscriptions.forEach(d => d.dispose());
}
public render() {
const readOnly = this.props.readOnly;
const waterMarkClass = this.props.showWatermark && this.state.allowWatermark && !readOnly ? 'code-watermark' : 'hide';
const classes = readOnly ? 'code-area' : 'code-area code-area-editable';
const options: monacoEditor.editor.IEditorConstructionOptions = {
minimap: {
enabled: false
},
glyphMargin: false,
wordWrap: 'on',
scrollBeyondLastLine: false,
scrollbar: {
vertical: 'hidden',
horizontal: 'hidden'
},
lineNumbers: 'off',
renderLineHighlight: 'none',
highlightActiveIndentGuide: false,
autoIndent: true,
autoClosingBrackets: this.props.testMode ? 'never' : 'languageDefined',
autoClosingQuotes: this.props.testMode ? 'never' : 'languageDefined',
renderIndentGuides: false,
overviewRulerBorder: false,
overviewRulerLanes: 0,
hideCursorInOverviewRuler: true,
folding: false,
readOnly: readOnly,
lineDecorationsWidth: 0,
contextmenu: false,
matchBrackets: false,
...this.props.editorOptions
};
return (
<div className={classes}>
<MonacoEditor
testMode={this.props.testMode}
value={this.props.code}
outermostParentClass={this.props.outermostParentClass}
theme={this.props.monacoTheme ? this.props.monacoTheme : 'vs'}
language='python'
editorMounted={this.editorDidMount}
options={options}
openLink={this.props.openLink}
ref={this.editorRef}
forceBackground={this.props.forceBackgroundColor}
/>
<div className={waterMarkClass}>{this.getWatermarkString()}</div>
</div>
);
}
public onParentClick(ev: React.MouseEvent<HTMLDivElement>) {
const readOnly = this.props.testMode || this.props.readOnly;
if (this.state.editor && !readOnly) {
ev.stopPropagation();
this.state.editor.focus();
}
}
public giveFocus() {
const readOnly = this.props.testMode || this.props.readOnly;
if (this.state.editor && !readOnly) {
this.state.editor.focus();
}
}
private getWatermarkString = () : string => {
return getLocString('DataScience.inputWatermark', 'Type code here and press shift-enter to run');
}
private editorDidMount = (editor: monacoEditor.editor.IStandaloneCodeEditor) => {
// Update our state
const model = editor.getModel();
this.setState({ editor, model: editor.getModel() });
// Listen for model changes
this.subscriptions.push(editor.onDidChangeModelContent(this.modelChanged));
// List for key up/down events if not read only
if (!this.props.readOnly) {
this.subscriptions.push(editor.onKeyDown(this.onKeyDown));
this.subscriptions.push(editor.onKeyUp(this.onKeyUp));
}
// Indicate we're ready
this.props.onCreated(this.props.code, model!.id);
}
private modelChanged = (e: monacoEditor.editor.IModelContentChangedEvent) => {
if (this.state.model) {
this.props.onChange(e.changes, this.state.model.id);
}
if (!this.props.readOnly && this.state.model) {
this.setState({allowWatermark: this.state.model.getValueLength() === 0});
}
}
private onKeyDown = (e: monacoEditor.IKeyboardEvent) => {
if (e.shiftKey && e.keyCode === monacoEditor.KeyCode.Enter && this.state.model && this.state.editor) {
// Shift enter was hit
e.stopPropagation();
e.preventDefault();
window.setTimeout(this.submitContent, 0);
} else if (e.keyCode === monacoEditor.KeyCode.UpArrow) {
this.arrowUp(e);
} else if (e.keyCode === monacoEditor.KeyCode.DownArrow) {
this.arrowDown(e);
}
}
private onKeyUp = (e: monacoEditor.IKeyboardEvent) => {
if (e.shiftKey && e.keyCode === monacoEditor.KeyCode.Enter) {
// Shift enter was hit
e.stopPropagation();
e.preventDefault();
}
}
private submitContent = () => {
let content = this.getContents();
if (content) {
// Remove empty lines off the end
let endPos = content.length - 1;
while (endPos >= 0 && content[endPos] === '\n') {
endPos -= 1;
}
content = content.slice(0, endPos + 1);
// Send to the input history too if necessary
if (this.props.history) {
this.props.history.add(content, this.state.model!.getVersionId() > this.lastCleanVersionId);
}
// Clear our current contents since we submitted
this.state.model!.setValue('');
// Send to jupyter
this.props.onSubmit(content);
}
}
private getContents() : string {
if (this.state.model) {
return this.state.model.getValue().replace(/\r/g, '');
}
return '';
}
private isAutoCompleteOpen() : boolean {
if (this.editorRef.current) {
return this.editorRef.current.isSuggesting();
}
return false;
}
private arrowUp(e: monacoEditor.IKeyboardEvent) {
if (this.state.editor && this.state.model && !this.isAutoCompleteOpen()) {
const cursor = this.state.editor.getPosition();
if (cursor && cursor.lineNumber === 1 && this.props.history) {
const currentValue = this.getContents();
const newValue = this.props.history.completeUp(currentValue);
if (newValue !== currentValue) {
this.state.model.setValue(newValue);
this.lastCleanVersionId = this.state.model.getVersionId();
this.state.editor.setPosition({lineNumber: 1, column: 1});
e.stopPropagation();
}
}
}
}
private arrowDown(e: monacoEditor.IKeyboardEvent) {
if (this.state.editor && this.state.model && !this.isAutoCompleteOpen()) {
const cursor = this.state.editor.getPosition();
if (cursor && cursor.lineNumber === this.state.model.getLineCount() && this.props.history) {
const currentValue = this.getContents();
const newValue = this.props.history.completeDown(currentValue);
if (newValue !== currentValue) {
this.state.model.setValue(newValue);
this.lastCleanVersionId = this.state.model.getVersionId();
const lastLine = this.state.model.getLineCount();
this.state.editor.setPosition({lineNumber: lastLine, column: this.state.model.getLineLength(lastLine) + 1});
e.stopPropagation();
}
}
}
}
}