Skip to content

Commit ca117d8

Browse files
committed
add docs & tests for editor. improve editor open perf
1 parent e10e386 commit ca117d8

File tree

14 files changed

+123
-91
lines changed

14 files changed

+123
-91
lines changed

lib/modules/alert/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
var types_1 = require('./types');
3-
var colors = {
3+
exports.colors = {
44
PASS: '#73C990',
55
FAIL: '#FF4081',
66
NOTE: '#9DA5B4',
@@ -10,7 +10,7 @@ exports._alert = {
1010
open: false,
1111
action: 'NOTE',
1212
duration: 1500,
13-
color: colors.NOTE
13+
color: exports.colors.NOTE
1414
};
1515
var open = {
1616
open: true,
@@ -19,7 +19,7 @@ var open = {
1919
};
2020
var current = exports._alert;
2121
function setAlert(a) {
22-
a.color = colors[a.action] || colors.NOTE;
22+
a.color = exports.colors[a.action] || exports.colors.NOTE;
2323
var statusBarAlert = document.getElementsByClassName('cr-alert-replay')[0];
2424
statusBarAlert.style.color = a.color;
2525
current = a;

lib/modules/editor/actions/file.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ exports.save = save;
1111
function open(file, options) {
1212
if (options === void 0) { options = {}; }
1313
return new Promise(function (resolve, reject) {
14-
var openTimeout = 300;
1514
atom.workspace.open(file, options);
16-
setTimeout(function () { return resolve(); }, openTimeout);
15+
atom.workspace.onDidOpen(function () { return resolve(); });
1716
});
1817
}
1918
exports.open = open;

lib/modules/editor/actions/writeFile.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var path_1 = require('path');
55
function writeFileFromContent(_a) {
66
var to = _a.to, content = _a.content, dir = _a.dir;
77
var toAbs = path_1.join(dir, to);
8-
createFolders({ dir: dir, to: to }).then(function () {
8+
createFolder({ dir: dir, to: to }).then(function () {
99
fs_1.writeFile(toAbs, content, function (writeErr) {
1010
if (writeErr) {
1111
console.log("Error: tried but failed to write to " + toAbs + " with: " + content, writeErr);
@@ -19,7 +19,7 @@ function writeFileFromFile(_a) {
1919
var to = _a.to, from = _a.from, dir = _a.dir, tutorialDir = _a.tutorialDir;
2020
var toAbs = path_1.join(dir, to);
2121
var fromAbs = path_1.join(tutorialDir, from);
22-
createFolders({ dir: dir, to: to }).then(function () {
22+
createFolder({ dir: dir, to: to }).then(function () {
2323
fs_1.readFile(fromAbs, 'utf8', function (readErr, data) {
2424
var err = "Error: tried to write '" + fromAbs + "' to '" + toAbs + "' but failed.";
2525
if (readErr) {
@@ -35,7 +35,7 @@ function writeFileFromFile(_a) {
3535
});
3636
}
3737
exports.writeFileFromFile = writeFileFromFile;
38-
function createFolders(_a) {
38+
function createFolder(_a) {
3939
var dir = _a.dir, to = _a.to;
4040
return new Promise(function (resolve, reject) {
4141
var folders = to.split('/').slice(0, -1);

src/modules/dir/reducer.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/// <reference path="../../typings/globals/jest/index.d.ts" />
2+
/// <reference path="../../typings/common/global.d.ts" />
3+
14
import dir from './index';
25
import {atomPath} from '../../__tests__/mocks/atom';
36

@@ -10,11 +13,11 @@ describe('dir reducer', () => {
1013
it('should return the project directory in Atom', () => {
1114
const pathToDir = './path/to/dir';
1215
global.atom = atomPath(pathToDir);
13-
expect(dir()).toBe(pathToDir);
16+
expect(dir('')).toBe(pathToDir);
1417
});
1518

1619
it('should throw an error if no project directory is found', () => {
17-
expect(() => dir()).toThrowError(/No project directory/);
20+
expect(() => dir('')).toThrowError(/No project directory/);
1821
});
1922

2023
});

src/modules/editor/Save/index.tsx

-33
This file was deleted.

src/modules/editor/ToggleDevTools/index.tsx

-32
This file was deleted.

src/modules/editor/actions/console.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
/**
2+
* Toggle atom devtools console
3+
* @returns void
4+
*/
15
export function toggleDevTools(): void {
26
atom.toggleDevTools();
37
}
48

9+
/**
10+
* Clear atom devtools console
11+
* @returns void
12+
*/
513
export function clearConsole(): void {
614
atom.executeJavaScriptInDevTools(console.clear());
715
}
816

17+
/**
18+
* Open atom devtools
19+
* @returns void
20+
*/
921
export function openDevTools(): void {
1022
atom.openDevTools();
1123
}

src/modules/editor/actions/editor.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Get the current active atom editor
3+
* @returns Promise
4+
*/
15
export function getEditor(): Promise<AtomCore.IEditor> {
26
return new Promise((resolve, reject) => {
37
const editor = atom.workspace.getActiveTextEditor();

src/modules/editor/actions/file.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,44 @@ import {unlink} from 'fs';
33
import {getEditor} from './editor';
44
import fileExists from 'node-file-exists';
55

6+
/**
7+
* Open a new folder in atom
8+
* @returns void
9+
*/
610
export function openFolder(): void {
711
atom.open();
812
}
913

10-
export function save() {
14+
/**
15+
* Saves the current editor
16+
* @returns void
17+
*/
18+
export function save(): void {
1119
getEditor().then(editor => editor.save());
1220
}
1321

22+
/**
23+
* Opens a file
24+
* https://atom.io/docs/api/v1.10.2/Workspace#instance-open
25+
* @param {string} file file name
26+
* @param {} options={} file open options
27+
* @returns Promise
28+
*/
1429
export function open(file: string, options = {}): Promise<any> {
1530
return new Promise((resolve, reject) => {
16-
// delete file first, to avoid bug
17-
// if (fileExists(file)) {
18-
// unlink(file);
19-
// }
20-
// delay necessary since opening a file is slow
21-
const openTimeout = 300;
2231
atom.workspace.open(file, options);
23-
setTimeout(() => resolve(), openTimeout);
32+
// resolve when file opens
33+
// https://atom.io/docs/api/v1.10.2/Workspace#instance-onDidOpen
34+
atom.workspace.onDidOpen(() => resolve());
2435
});
2536
}
2637

27-
export function scroll(content: string): Promise<void> {
38+
/**
39+
* Scroll to cursor position
40+
* @param {string} content text editor content
41+
* @returns Promise
42+
*/
43+
export function scroll(content: string): any {
2844
return getEditor().then((editor: AtomCore.IEditor) => {
2945
const regex = new RegExp(
3046
content.replace(/[\\\.\+\*\?\^\$\[\]\(\)\{\}\/\'\#\:\!\=\|]/ig, '\\$&'), 'gm'

src/modules/editor/actions/tabs.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* close all other tabs
3+
* @returns void
4+
*/
15
export function closeAllPanels(): void {
26
let editors: AtomCore.IEditor[] = atom.workspace.getTextEditors();
37
editors.forEach((editor: AtomCore.IEditor) => {

src/modules/editor/actions/write.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import {getEditor} from './editor';
22

3-
function write(action: 'set' | 'insert', text: string, options = {}) {
3+
/**
4+
* add text to the active editor
5+
* @param {'set'|'insert'} action
6+
* @param {string} text
7+
* @param {} options={}
8+
* @result Promise
9+
*/
10+
function write(action: 'set' | 'insert', text: string, options = {}): Promise<any> {
411
return getEditor().then((editor: AtomCore.IEditor) => {
512
editor.moveToBottom();
613
editor[`${action}Text`](text, options);
@@ -11,16 +18,31 @@ function write(action: 'set' | 'insert', text: string, options = {}) {
1118
});
1219
}
1320

14-
// Set text, removes any previous content in file
21+
/**
22+
* set text
23+
* https://atom.io/docs/api/v1.10.2/TextEditor#instance-setText
24+
* @param {string} text
25+
*/
1526
export function set(text: string) {
1627
return write('set', text);
1728
}
1829

30+
/**
31+
* insert text
32+
* https://atom.io/docs/api/v1.10.2/TextEditor#instance-insertText
33+
* @param {string} text
34+
* @param {} options={}
35+
*/
1936
export function insert(text: string, options = {}) {
2037
return write('insert', text, options);
2138
}
2239

2340
const cursor: RegExp = /::>/g;
41+
42+
/**
43+
* replace ::> with cursor position
44+
* @param {AtomCore.IEditor} editor
45+
*/
2446
function setCursorPosition(editor: AtomCore.IEditor) {
2547
return editor.scan(cursor, (scanned) => {
2648
editor.setCursorScreenPosition(scanned.range.start);

src/modules/editor/actions/writeFile.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ import { mkdirSync, readFile, writeFile } from 'fs';
22
import fileExists from 'node-file-exists';
33
import { join } from 'path';
44

5-
export function writeFileFromContent({to, content, dir}) {
5+
/**
6+
* writes content to a user file
7+
* @param {} {to user file path
8+
* @param {} content text editor content
9+
* @param {} dir} user directory
10+
*/
11+
export function writeFileFromContent({to, content, dir}): void {
612
const toAbs = join(dir, to);
7-
createFolders({dir, to}).then(() => {
13+
createFolder({dir, to}).then(() => {
814
writeFile(toAbs, content, (writeErr) => {
915
if (writeErr) {
1016
console.log(`Error: tried but failed to write to ${toAbs} with: ${content}`, writeErr);
@@ -14,11 +20,19 @@ export function writeFileFromContent({to, content, dir}) {
1420
});
1521
}
1622

17-
export function writeFileFromFile({to, from, dir, tutorialDir}) {
23+
/**
24+
* writes from a tutorial file to a user file
25+
* @param {} {to user file path
26+
* @param {} from tutorial file path
27+
* @param {} dir user directory
28+
* @param {} tutorialDir} tutorial directory
29+
* @returns void
30+
*/
31+
export function writeFileFromFile({to, from, dir, tutorialDir}): void {
1832
const toAbs = join(dir, to);
1933
const fromAbs = join(tutorialDir, from);
2034

21-
createFolders({dir, to}).then(() => {
35+
createFolder({dir, to}).then(() => {
2236
// writes { to: './dest.js', from: '' }
2337
readFile(fromAbs, 'utf8', (readErr, data) => {
2438
const err = `Error: tried to write '${fromAbs}' to '${toAbs}' but failed.`;
@@ -31,7 +45,12 @@ export function writeFileFromFile({to, from, dir, tutorialDir}) {
3145
});
3246
}
3347

34-
function createFolders({dir, to}) {
48+
/**
49+
* create user folder
50+
* @param {} {dir user directory
51+
* @param {} to} user folder path
52+
*/
53+
function createFolder({dir, to}): Promise<any> {
3554
return new Promise((resolve, reject) => {
3655
// extract folders without final file name
3756
const folders = to.split('/').slice(0, -1);

src/modules/editor/reducer.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="../../typings/globals/jest/index.d.ts" />
2+
import reducer from './reducer';
3+
import * as types from './types';
4+
5+
describe('editor reducer', () => {
6+
7+
it('does nothing if action types do not match', () => {
8+
const action = { type: 'unknown' };
9+
expect(reducer(undefined, action)).toBe('atom');
10+
});
11+
12+
});

src/modules/editor/reducer.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import {
33
} from './types';
44
import {insert, open, save, scroll, set, toggleDevTools, writeFileFromContent, writeFileFromFile} from './index';
55

6+
/**
7+
* Editor Reducer triggers editor actions
8+
* @param {} editor='atom'
9+
* @param {Action} action
10+
* @returns string editor name
11+
*/
612
export default function editor(
713
editor = 'atom', action: Action
814
): string {

0 commit comments

Comments
 (0)