Skip to content

Commit c93d997

Browse files
committed
fix getEditor cannot find issue
1 parent 959dcb2 commit c93d997

File tree

7 files changed

+144
-10
lines changed

7 files changed

+144
-10
lines changed

lib/atom/actions/editor.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ function getEditor() {
77
getEditorCount += 1;
88
setTimeout(function () {
99
editor = atom.workspace.getActiveTextEditor();
10-
});
11-
if (getEditorCount > 999) {
12-
console.log('Failed to find active editor');
13-
reject(null);
10+
}, 10);
11+
if (getEditorCount > 1000) {
12+
console.log('Cannot find active text editor');
13+
setTimeout(function () {
14+
editor = atom.workspace.getActiveTextEditor();
15+
}, 100);
1416
}
1517
}
1618
resolve(editor);

lib/atom/actions/file.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ function open(filePath, options) {
1616
if (exists_1.fileExists(filePath)) {
1717
fs_1.unlink(filePath);
1818
}
19-
resolve(atom.workspace.open(filePath, options));
19+
atom.workspace.open(filePath, options);
20+
setTimeout(function () {
21+
resolve();
22+
}, 200);
2023
});
2124
}
2225
exports.open = open;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"use strict";
2+
var editor_1 = require('../../atom/editor');
3+
var action_helpers_1 = require('./action-helpers');
4+
var Type = {
5+
OPEN: 'open',
6+
SET: 'set',
7+
INSERT: 'insert',
8+
OPEN_CONSOLE: 'openConsole',
9+
};
10+
function editorActions(actionString) {
11+
return new Promise(function (resolve, reject) {
12+
var command = action_helpers_1.getCommand(actionString);
13+
var params = action_helpers_1.getParams(actionString);
14+
switch (command) {
15+
case Type.OPEN:
16+
var obj = action_helpers_1.getOptions(params[0]);
17+
var file = obj.param;
18+
var options = obj.options;
19+
if (params.length === 1) {
20+
editor_1.open(file, options);
21+
setTimeout(function () {
22+
resolve();
23+
}, 100);
24+
}
25+
break;
26+
case Type.SET:
27+
if (params.length === 1) {
28+
var content_1 = params[0];
29+
setTimeout(function () {
30+
editor_1.set(content_1);
31+
resolve(true);
32+
});
33+
}
34+
break;
35+
case Type.INSERT:
36+
if (params.length === 1) {
37+
var content_2 = params[0];
38+
setTimeout(function () {
39+
editor_1.insert(content_2, {});
40+
resolve(true);
41+
});
42+
}
43+
break;
44+
case Type.OPEN_CONSOLE:
45+
if (params.length === 0) {
46+
setTimeout(function () {
47+
editor_1.openDevTools();
48+
resolve(true);
49+
});
50+
}
51+
break;
52+
default:
53+
console.log('Invalid editor action command');
54+
reject(false);
55+
}
56+
}).catch(function (err) {
57+
console.error('Error with editor', err);
58+
});
59+
}
60+
exports.editorActions = editorActions;

src/atom/actions/editor.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ export function getEditor(): Promise<AtomCore.IEditor> {
66
getEditorCount += 1;
77
setTimeout(function() {
88
editor = atom.workspace.getActiveTextEditor();
9-
});
10-
if (getEditorCount > 999) {
11-
console.log('Failed to find active editor');
12-
reject(null);
9+
}, 10);
10+
if (getEditorCount > 1000) {
11+
console.log('Cannot find active text editor');
12+
setTimeout(() => {
13+
editor = atom.workspace.getActiveTextEditor();
14+
}, 100);
1315
}
1416
}
1517
resolve(editor);

src/atom/actions/file.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export function open(filePath: string, options = {}): Promise<any> {
1616
if (fileExists(filePath)) {
1717
unlink(filePath);
1818
}
19-
resolve(atom.workspace.open(filePath, options));
19+
atom.workspace.open(filePath, options);
20+
setTimeout(() => {
21+
resolve();
22+
}, 200); // delay necessary since opening a file is slow
2023
});
2124
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {open, set, insert, openDevTools} from '../../atom/editor';
2+
import {getCommand, getParams, getOptions} from './action-helpers';
3+
4+
const Type = {
5+
OPEN: 'open',
6+
SET: 'set',
7+
INSERT: 'insert',
8+
OPEN_CONSOLE: 'openConsole',
9+
};
10+
11+
export function editorActions(actionString: string): Promise<void> {
12+
return new Promise((resolve, reject) => {
13+
let command: string = getCommand(actionString);
14+
let params: string[] = getParams(actionString);
15+
switch (command) {
16+
case Type.OPEN:
17+
let obj = getOptions(params[0]);
18+
let file = obj.param;
19+
let options = obj.options;
20+
if (params.length === 1) {
21+
open(file, options);
22+
setTimeout(function() {
23+
resolve();
24+
}, 100);
25+
}
26+
break;
27+
case Type.SET:
28+
if (params.length === 1) {
29+
let content = params[0];
30+
31+
setTimeout(function() {
32+
set(content);
33+
resolve(true);
34+
});
35+
}
36+
break;
37+
case Type.INSERT:
38+
if (params.length === 1) {
39+
// let obj = getOptions(params[0]);
40+
let content: string = params[0];
41+
// let options: object = obj.options;
42+
setTimeout(function() {
43+
insert(content, {});
44+
resolve(true);
45+
});
46+
}
47+
break;
48+
case Type.OPEN_CONSOLE:
49+
if (params.length === 0) {
50+
setTimeout(function() {
51+
openDevTools();
52+
resolve(true);
53+
});
54+
}
55+
break;
56+
default:
57+
console.log('Invalid editor action command');
58+
reject(false);
59+
}
60+
}).catch((err) => {
61+
console.error('Error with editor', err);
62+
});
63+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"src/reducers/checks/verify.ts",
8686
"src/reducers/dir/index.ts",
8787
"src/reducers/editor-actions/action-helpers.ts",
88+
"src/reducers/editor-actions/action-reducer.ts",
8889
"src/reducers/editor-actions/actions.ts",
8990
"src/reducers/editor-actions/index.ts",
9091
"src/reducers/editor-actions/parser.ts",

0 commit comments

Comments
 (0)