Skip to content

Commit 4e04d8e

Browse files
errors, corrections, remove fading
1 parent f4b584d commit 4e04d8e

File tree

7 files changed

+196
-96
lines changed

7 files changed

+196
-96
lines changed

packages/app/src/app/overmind/effects/vscode/ModelsHandler.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
getModulePath,
3-
resolveModule,
4-
} from '@codesandbox/common/lib/sandbox/modules';
1+
import { resolveModule } from '@codesandbox/common/lib/sandbox/modules';
52
import {
63
EditorSelection,
74
Module,
@@ -13,7 +10,7 @@ import { actions, dispatch } from 'codesandbox-api';
1310
import { css } from 'glamor';
1411
import { TextOperation } from 'ot';
1512

16-
import { getCurrentModelPath, getModel, getVSCodePath } from './utils';
13+
import { getCurrentModelPath, getModel } from './utils';
1714

1815
// @ts-ignore
1916
const fadeIn = css.keyframes('fadeIn', {
@@ -34,6 +31,7 @@ export type OnFileChangeData = {
3431
title: string;
3532
code: string;
3633
event?: any[];
34+
model?: any;
3735
};
3836

3937
export type OnFileChangeCallback = (data: OnFileChangeData) => void;
@@ -147,6 +145,7 @@ export class ModelsHandler {
147145
code: model.object.textEditorModel.getValue(),
148146
moduleShortid: module.shortid,
149147
title: module.title,
148+
model,
150149
});
151150
}
152151
});
@@ -462,6 +461,7 @@ export class ModelsHandler {
462461
title: module.title,
463462
code: model.getValue(),
464463
event: e,
464+
model,
465465
});
466466
} catch (err) {
467467
if (process.env.NODE_ENV === 'development') {

packages/app/src/app/overmind/effects/vscode/Workbench.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,8 @@ export class Workbench {
400400
) {
401401
this.monaco.editor.appendMenuItem(menubarId, item);
402402
}
403+
404+
private addNotification(notification: NotificationMessage) {
405+
notificationState.addNotification(notification);
406+
}
403407
}

packages/app/src/app/overmind/effects/vscode/index.ts

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ import { resolveModule } from '@codesandbox/common/lib/sandbox/modules';
33
import {
44
EditorSelection,
55
Module,
6+
ModuleCorrection,
7+
ModuleError,
68
Sandbox,
79
SandboxFs,
810
Settings,
911
} from '@codesandbox/common/lib/types';
10-
import {
11-
convertTypeToStatus,
12-
notificationState,
13-
} from '@codesandbox/common/lib/utils/notifications';
14-
import { NotificationMessage } from '@codesandbox/notifications/lib/state';
1512
import { Reaction } from 'app/overmind';
1613
import prettify from 'app/src/app/utils/prettify';
1714
import { blocker } from 'app/utils/blocker';
@@ -28,7 +25,7 @@ import {
2825
import { Linter } from './Linter';
2926
import { ModelsHandler, OnFileChangeData } from './ModelsHandler';
3027
import sandboxFsSync from './sandboxFsSync';
31-
import { getCode, getModel, getSelection, getVSCodePath } from './utils';
28+
import { getSelection } from './utils';
3229
import loadScript from './vscode-script-loader';
3330
import { Workbench } from './Workbench';
3431

@@ -255,14 +252,92 @@ export class VSCodeEffect {
255252
await this.initialized;
256253
const model = await this.modelsHandler.changeModule(module);
257254

258-
this.linter.lint(
259-
model.getValue(),
260-
module.title,
261-
model.getVersionId(),
262-
this.options.getCurrentSandbox().template
263-
);
255+
this.lint(module.title, model);
264256
}
265257

258+
setErrors = (errors: ModuleError[]) => {
259+
const activeEditor = this.editorApi.getActiveCodeEditor();
260+
261+
if (activeEditor) {
262+
if (errors.length > 0) {
263+
const currentPath = this.getCurrentModelPath();
264+
const thisModuleErrors = errors.filter(
265+
error => error.path === currentPath
266+
);
267+
const errorMarkers = thisModuleErrors
268+
.map(error => {
269+
if (error) {
270+
return {
271+
severity: this.monaco.MarkerSeverity.Error,
272+
startColumn: 1,
273+
startLineNumber: error.line,
274+
endColumn: error.column,
275+
endLineNumber: error.line + 1,
276+
message: error.message,
277+
};
278+
}
279+
280+
return null;
281+
})
282+
.filter(x => x);
283+
284+
this.monaco.editor.setModelMarkers(
285+
activeEditor.getModel(),
286+
'error',
287+
errorMarkers
288+
);
289+
} else {
290+
this.monaco.editor.setModelMarkers(
291+
activeEditor.getModel(),
292+
'error',
293+
[]
294+
);
295+
}
296+
}
297+
};
298+
299+
setCorrections = (corrections: ModuleCorrection[]) => {
300+
const activeEditor = this.editorApi.getActiveCodeEditor();
301+
if (activeEditor) {
302+
if (corrections.length > 0) {
303+
const currentPath = this.getCurrentModelPath();
304+
const correctionMarkers = corrections
305+
.filter(correction => correction.path === currentPath)
306+
.map(correction => {
307+
if (correction) {
308+
return {
309+
severity:
310+
correction.severity === 'warning'
311+
? this.monaco.MarkerSeverity.Warning
312+
: this.monaco.MarkerSeverity.Notice,
313+
startColumn: correction.column,
314+
startLineNumber: correction.line,
315+
endColumn: correction.columnEnd || 1,
316+
endLineNumber: correction.lineEnd || correction.line + 1,
317+
message: correction.message,
318+
source: correction.source,
319+
};
320+
}
321+
322+
return null;
323+
})
324+
.filter(x => x);
325+
326+
this.monaco.editor.setModelMarkers(
327+
activeEditor.getModel(),
328+
'correction',
329+
correctionMarkers
330+
);
331+
} else {
332+
this.monaco.editor.setModelMarkers(
333+
activeEditor.getModel(),
334+
'correction',
335+
[]
336+
);
337+
}
338+
}
339+
};
340+
266341
private async disableExtension(id: string) {
267342
const extensionService = await this.extensionService.promise;
268343
const extensionEnablementService = await this.extensionEnablementService
@@ -552,6 +627,8 @@ export class VSCodeEffect {
552627
return;
553628
}
554629

630+
this.lint(data.title, data.model);
631+
555632
this.options.onCodeChange(data);
556633
};
557634

@@ -649,6 +726,29 @@ export class VSCodeEffect {
649726
}
650727
});
651728
}
729+
730+
private lint(title: string, model: any) {
731+
this.linter.lint(
732+
model.getValue(),
733+
title,
734+
model.getVersionId(),
735+
this.options.getCurrentSandbox().template
736+
);
737+
}
738+
739+
private getCurrentModelPath = () => {
740+
const activeEditor = this.editorApi.getActiveCodeEditor();
741+
742+
if (!activeEditor) {
743+
return undefined;
744+
}
745+
const model = activeEditor.getModel();
746+
if (!model) {
747+
return undefined;
748+
}
749+
750+
return model.uri.path.replace(/^\/sandbox/, '');
751+
};
652752
}
653753

654754
export default new VSCodeEffect();

packages/app/src/app/overmind/effects/vscode/sandboxFsSync/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ class SandboxFsSync {
4242
private types: any;
4343
private typesInfo: Promise<any>;
4444
private initializingWorkers = blocker<void>();
45-
// We do not want to send initial sandbox until
46-
// all 3 filesystems are running
4745
private workersInitializedCount = 0;
4846
public initialize(options: SandboxFsSyncOptions) {
4947
this.options = options;
5048
self.addEventListener('message', evt => {
49+
// We do not want to send initial sandbox until
50+
// all 3 filesystems are running
5151
if (this.initializingWorkers.isResolved()) {
5252
if (evt.data.$type === 'sync-types') {
5353
this.syncDependencyTypings();
@@ -117,6 +117,7 @@ class SandboxFsSync {
117117
public async sync() {
118118
await this.initializingWorkers.promise;
119119

120+
// eslint-disable-next-line
120121
console.log('## SYNCING SANDBOX AND TYPINGS WITH WORKERS');
121122
this.syncSandbox();
122123

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { resolveModule } from '@codesandbox/common/lib/sandbox/modules';
2-
import { getHashedUserId } from '@codesandbox/common/lib/utils/analytics';
32
import {
43
EnvironmentVariable,
54
ModuleCorrection,
65
ModuleError,
76
ModuleTab,
87
WindowOrientation,
98
} from '@codesandbox/common/lib/types';
9+
import { getHashedUserId } from '@codesandbox/common/lib/utils/analytics';
1010
import { Action, AsyncAction } from 'app/overmind';
1111
import { withLoadApp, withOwnedSandbox } from 'app/overmind/factories';
1212
import { sortObjectByKeys } from 'app/overmind/utils/common';
@@ -576,6 +576,7 @@ export const previewActionReceived: Action<{
576576

577577
module.errors.push(json(error));
578578
state.editor.errors.push(error);
579+
effects.vscode.setErrors(state.editor.errors);
579580
} catch (e) {
580581
/* ignore, this module can be in a node_modules for example */
581582
}
@@ -601,6 +602,7 @@ export const previewActionReceived: Action<{
601602

602603
state.editor.corrections.push(correction);
603604
module.corrections.push(json(correction));
605+
effects.vscode.setCorrections(state.editor.corrections);
604606
} catch (e) {
605607
/* ignore, this module can be in a node_modules for example */
606608
}
@@ -631,6 +633,7 @@ export const previewActionReceived: Action<{
631633
module.errors.push(error);
632634
});
633635
state.editor.errors = newErrors;
636+
effects.vscode.setErrors(state.editor.errors);
634637
}
635638
break;
636639
}
@@ -667,6 +670,7 @@ export const previewActionReceived: Action<{
667670
module.corrections.push(correction);
668671
});
669672
state.editor.corrections = newCorrections;
673+
effects.vscode.setCorrections(state.editor.corrections);
670674
}
671675
break;
672676
}

packages/app/src/app/overmind/namespaces/editor/internalActions.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ export const saveCode: AsyncAction<{
8888
moduleShortid,
8989
});
9090

91-
effects.vscode.fs.writeFile(state.editor.modulesByPath, module);
92-
9391
try {
9492
const updatedModule = await effects.api.saveModuleCode(sandbox.id, module);
9593

@@ -98,6 +96,7 @@ export const saveCode: AsyncAction<{
9896
module.savedCode =
9997
updatedModule.code === module.code ? null : updatedModule.code;
10098

99+
effects.vscode.fs.writeFile(state.editor.modulesByPath, module);
101100
effects.moduleRecover.remove(sandbox.id, module);
102101

103102
state.editor.changedModuleShortids.splice(

0 commit comments

Comments
 (0)