-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
149 lines (126 loc) · 5.83 KB
/
main.js
File metadata and controls
149 lines (126 loc) · 5.83 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
/*
ABOUT
This Brackets extenstion will execute a script named "go" in the root of the
currently open dir or project.
The output is parsed, displayed in a bottom panel. Stack trace lines with file
names and line number become clickable links that will point the editor to the
right location.
Stack trace parsing is Node.js specific.
*/
/* jshint laxcomma:true, asi:true, debug:true, unused:true */
/* global define, brackets, $ */
define(function (require, exports, module) {
"use strict";
// Load a bunch of Brackets modules
var CommandManager = brackets.getModule("command/CommandManager")
, EditorManager = brackets.getModule("editor/EditorManager")
, Menus = brackets.getModule("command/Menus")
, KeyBindingManager = brackets.getModule("command/KeyBindingManager")
, NodeConnection = brackets.getModule("utils/NodeConnection")
, ExtensionUtils = brackets.getModule("utils/ExtensionUtils")
, PanelManager = brackets.getModule("view/PanelManager")
, ProjectManager = brackets.getModule("project/ProjectManager")
, FileViewController = brackets.getModule("project/FileViewController")
var extenstionPath = ExtensionUtils.getModulePath(module)
, nodeRunnerPath = extenstionPath + 'node_runner' // Path to node_runner.js
, linkRe = /(\/.*js:\d+:\d+)/
, lastOutput
// Create the DOM element for bottom panel
ExtensionUtils.loadStyleSheet(module, "runner.css");
var panel_html = require("text!panel.html")
, panel = PanelManager.createBottomPanel("runner", $(panel_html))
// Hide the panel when the [x] in top rght corner is clicked.
$('#runner-panel-close').click(function () { panel.hide() });
// Register the command
var RUNNER_COMMAND_ID = "kamrik.brackets.runner"
CommandManager.register("Run runner", RUNNER_COMMAND_ID, handleRunner)
// Create a menu item.
// TODO: Move it to place more logical than next to File->Quit
var menu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
menu.addMenuItem(RUNNER_COMMAND_ID);
// TODO: What are the most common shortcuts for running the code? Ctrl-F9 is from my Borland Pascal days.
KeyBindingManager.addBinding(RUNNER_COMMAND_ID, { key: "Ctrl-F9" });
// Handle click on a trace line with file name and line number
// A typical trace looks lie this:
//
// Error: ENOENT, no such file or directory '/bad/file/path'
// at Object.openSync (fs.js:230:18)
// at Object.readFileSync (fs.js:120:15)
// at readMyFile (/path/to/some/code/example.js:10:6)
// at doStuff (/path/to/some/code/example.js:5:10)
// at Object.<anonymous> (/path/to/some/code/example.js:13:1)
// at Module._compile (module.js:441:26)
// at Object..js (module.js:459:10)
// at Module.load (module.js:348:31)
//
function onClick(e) {
var traceline = e.currentTarget.dataset.traceline
, parts = traceline.split(':')
, filePath = parts[0]
, line = parseInt(parts[1]) - 1
, ch = parseInt(parts[2])
, docPromise = FileViewController.openAndSelectDocument(filePath, FileViewController.PROJECT_MANAGER)
docPromise.then(function () {
var editor = EditorManager.getCurrentFullEditor()
if (editor) {
editor.setCursorPos(line, ch, true)
EditorManager.focusEditor()
}
})
.done()
}
// Handler for the File->Run command
function handleRunner() {
// root dir of the currently open project
var projectDir = ProjectManager.getProjectRoot().fullPath;
// path of the script to execute. TODO: make this configurable
var scriptPath = projectDir + '/go'
// Find the <table> element that will hold the output.
var $output = $('#runner-output')
$output.html('running...')
panel.show();
// Function to process output returned by the executable.
// result is an object, we only consider result.strdout. Redired stderr to stdout in your executable.
function processOutput(result) {
$output.html('')
lastOutput = result
var i, line
var text = result.stdout;
var lines = text.split('\n');
// Go over the output line by line
// convert parts like /path/file.js:99:99 to <a> elemtns.
// add the line to output panel and <tr><td><pre>line</pre></td></tr>.
for(i = 0; i < lines.length; i++) {
line = lines[i];
var m = linkRe.exec(line)
if (m) {
// Display the file name with the project path stripped for better readability.
var shortName = m[1].replace(projectDir, '')
line = line.replace(m[1], '<a href="#" data-traceline="' + m[1] + '">' + shortName + '</a>')
}
line = '<tr><td><pre>' + line + '</pre></td></tr>'
$output.append(line)
}
// Register the on-click handler for all <a> elements,
$('a', $output).click(onClick);
}
// Connect to the Brackets Node.js process, register the node side function and then run it.
// See: github.com/adobe/brackets/wiki/Brackets-Node-Process:-Overview-for-Developers
var nodeConnection = new NodeConnection();
nodeConnection.connect(true)
.then(function () {
return nodeConnection.loadDomains([nodeRunnerPath], true)
})
.then(function () {
return nodeConnection.domains.brunner.runAll(scriptPath)
})
.then(processOutput)
.fail(function (err) {
$output.html('<pre> Error communicating with node.js:\n' + err.toString() + '\n\n' + err.stack )
})
.always(function () {
//debugger
})
.done()
}
});