Skip to content

Commit 391c5d2

Browse files
committed
Open on Sourcegraph
0 parents  commit 391c5d2

7 files changed

Lines changed: 724 additions & 0 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Sourcegraph for Cloud9
2+
3+
The Sourcegraph plugin for Cloud9 editor let's you quickly open and search code on Sourcegraph.com or your Sourcegraph Server instance easily and efficiently.
4+
5+
## Installation
6+
7+
todo
8+
9+
## Usage
10+
11+
todo
12+
13+
## Settings
14+
15+
todo
16+
17+
## Development
18+
19+
todo

sourcegraph/c9build/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "sourcegraph",
3+
"version": "0.0.1",
4+
"author": "Sourcegraph",
5+
"contributors": [
6+
{
7+
"name": "Matt King",
8+
"email": "king@sourcegraph.com"
9+
}
10+
],
11+
"repository": {
12+
"type": "git",
13+
"url": ""
14+
},
15+
"plugins": {
16+
"sourcegraph": {}
17+
},
18+
"categories": ["miscellaneous"],
19+
"licenses": []
20+
}
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
define("plugins/sourcegraph/package.sourcegraph", [], {
2+
"name": "sourcegraph",
3+
"version": "0.0.1",
4+
"author": "Sourcegraph",
5+
"contributors": [
6+
{
7+
"name": "Matt King",
8+
"email": "king@sourcegraph.com"
9+
}
10+
],
11+
"repository": {
12+
"type": "git",
13+
"url": ""
14+
},
15+
"categories": [
16+
"miscellaneous"
17+
],
18+
"licenses": [],
19+
"c9": {
20+
"plugins": [
21+
{
22+
"packagePath": "plugins/sourcegraph/sourcegraph"
23+
}
24+
]
25+
}
26+
});
27+
28+
define("plugins/sourcegraph/sourcegraph",[], function(require, exports, module) {
29+
main.consumes = [
30+
'Plugin',
31+
'commands',
32+
'settings',
33+
'preferences',
34+
'proc',
35+
'c9',
36+
'tabManager'
37+
]
38+
main.provides = ['sourcegraph']
39+
return main
40+
41+
function main(options, imports, register) {
42+
var Plugin = imports.Plugin
43+
var commands = imports.commands
44+
var settings = imports.settings
45+
var prefs = imports.preferences
46+
var proc = imports.proc
47+
var workspaceDir = imports.c9.workspaceDir
48+
var tabs = imports.tabManager
49+
var VERSION = '0.0.1'
50+
51+
var plugin = new Plugin('Ajax.org', main.consumes)
52+
var emit = plugin.getEmitter()
53+
54+
function load() {
55+
addCommands()
56+
addPrefs()
57+
readSettings()
58+
}
59+
60+
function addPrefs() {
61+
prefs.add(
62+
{
63+
General: {
64+
position: 450,
65+
'Sourcegraph Server': {
66+
position: 100,
67+
URL: {
68+
type: 'textbox',
69+
setting: 'user/sourcegraph/@sourcegraph_url',
70+
position: 100
71+
}
72+
}
73+
}
74+
},
75+
plugin
76+
)
77+
}
78+
79+
function readSettings() {
80+
settings.on(
81+
'read',
82+
function() {
83+
settings.setDefaults('user/sourcegraph', [
84+
['sourcegraph_url', 'https://sourcegraph.com']
85+
])
86+
},
87+
plugin
88+
)
89+
}
90+
91+
function addCommands() {
92+
commands.addCommand(
93+
{
94+
name: 'sourcegraph_open',
95+
bindKey: {
96+
mac: 'Option-O',
97+
win: 'Alt-O'
98+
},
99+
exec: function() {
100+
repoInfo(function(info) {
101+
var tab = tabs.focussedTab
102+
var selection = tab.editor.ace.getSelectionRange()
103+
var { anchor, cursor } = getRangeForSelection(selection)
104+
105+
var startRow = anchor.row
106+
var startCol = anchor.column
107+
var endRow = cursor.row
108+
var endCol = cursor.column
109+
var sourcegraphUrl =
110+
settings.get('user/sourcegraph/@sourcegraph_url') ||
111+
'https://sourcegraph.com'
112+
var url = `${sourcegraphUrl.replace(
113+
/\/+$/,
114+
''
115+
)}/-/editor?remote_url=${encodeURIComponent(
116+
info.remote
117+
)}&branch=${encodeURIComponent(
118+
info.branch
119+
)}&file=${encodeURIComponent(
120+
info.file
121+
)}&editor=${encodeURIComponent(
122+
'Cloud9'
123+
)}&version=${VERSION}&start_row=${startRow}&start_col=${startCol}&end_row=${endRow}&end_col=${endCol}`
124+
window.open(url, '_blank')
125+
})
126+
},
127+
isAvailable: function(editor) {
128+
if (editor && editor.ace) {
129+
return true
130+
}
131+
}
132+
},
133+
plugin
134+
)
135+
136+
commands.addCommand(
137+
{
138+
name: 'sourcegraph_search',
139+
bindKey: {
140+
mac: 'Option-S',
141+
win: 'Alt-S'
142+
},
143+
exec: function() {
144+
repoInfo(function(info) {
145+
var sourcegraphUrl =
146+
settings.get('user/sourcegraph/@sourcegraph_url') ||
147+
'https://sourcegraph.com'
148+
var tab = tabs.focussedTab
149+
var editor = tab.editor.ace
150+
var query = editor.session.getTextRange(
151+
editor.getSelectionRange()
152+
)
153+
var url = `${sourcegraphUrl.replace(
154+
/\/+$/,
155+
''
156+
)}/-/editor?remote_url=${encodeURIComponent(
157+
info.remote
158+
)}&branch=${encodeURIComponent(
159+
info.branch
160+
)}&file=${encodeURIComponent(
161+
info.file
162+
)}&editor=${encodeURIComponent(
163+
'Cloud9'
164+
)}&version=${VERSION}&start_row=${
165+
info.startRow
166+
}&search=${encodeURIComponent(query)}`
167+
window.open(url, '_blank')
168+
})
169+
},
170+
isAvailable: function(editor) {
171+
if (editor && editor.ace) return !editor.ace.selection.isEmpty()
172+
return false
173+
}
174+
},
175+
plugin
176+
)
177+
}
178+
179+
function repoInfo(callback) {
180+
getRemote(function(err, remotes) {
181+
if (err) {
182+
return
183+
}
184+
var remote = remotes[0]
185+
gitRemoteUrl(remote, function(err, remoteUrl) {
186+
if (err) {
187+
return
188+
}
189+
getBranch(function(err, branch) {
190+
if (err) {
191+
return
192+
}
193+
var tab = tabs.focussedTab
194+
var filePath = tab && tab.path
195+
callback({
196+
remote: remoteUrl,
197+
branch: branch,
198+
file: filePath
199+
})
200+
})
201+
})
202+
})
203+
}
204+
205+
function buffer(process, callback) {
206+
var stdout = ''
207+
var stderr = ''
208+
process.stdout.on('data', function(c) {
209+
stdout += c
210+
})
211+
process.stderr.on('data', function(c) {
212+
stderr += c
213+
})
214+
process.on('exit', function(c) {
215+
callback(stdout, stderr)
216+
})
217+
}
218+
219+
function git(args, callback) {
220+
if (typeof args == 'string') args = args.split(/\s+/)
221+
222+
proc.spawn(
223+
'git',
224+
{
225+
args: args,
226+
cwd: workspaceDir
227+
},
228+
function(e, p) {
229+
buffer(p, function(stdout, stderr) {
230+
callback && callback(e, stdout, stderr)
231+
})
232+
}
233+
)
234+
}
235+
236+
function getBranch(callback) {
237+
git(['rev-parse', '--abbrev-ref', 'HEAD'], function(err, stdout, stderr) {
238+
if (err || stderr) return callback(err || stderr)
239+
callback(null, stdout.trim())
240+
})
241+
}
242+
243+
function getRemote(callback) {
244+
git(['remote'], function(err, stdout, stderr) {
245+
if (err || stderr) return callback(err || stderr)
246+
callback(null, stdout.split('\n'))
247+
})
248+
}
249+
250+
function gitRemoteUrl(remoteName, callback) {
251+
return git(['remote', 'get-url', remoteName], function(
252+
err,
253+
stdout,
254+
stderr
255+
) {
256+
if (err || stderr) return callback(err || stderr)
257+
callback(null, stdout.trim())
258+
})
259+
}
260+
261+
function getRangeForSelection(selection) {
262+
var cursor
263+
var anchor
264+
if (selection.start.row === selection.end.row) {
265+
if (selection.start.column < selection.end.column) {
266+
anchor = selection.start
267+
cursor = selection.end
268+
} else {
269+
anchor = selection.end
270+
cursor = selection.start
271+
}
272+
} else if (selection.start.row < selection.end.row) {
273+
anchor = selection.start
274+
cursor = selection.end
275+
} else {
276+
anchor = selection.end
277+
cursor = selection.start
278+
}
279+
return { cursor, anchor }
280+
}
281+
282+
plugin.on('load', function() {
283+
load()
284+
})
285+
plugin.on('unload', function() {})
286+
287+
plugin.freezePublicAPI({})
288+
289+
register(null, {
290+
sourcegraph: plugin
291+
})
292+
}
293+
})
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use client";
2+
"use mocha";
3+
4+
define(function(require, exports, module) {
5+
main.consumes = ["plugin.test", "sourcegraph"];
6+
main.provides = [];
7+
return main;
8+
9+
function main(options, imports, register) {
10+
var test = imports["plugin.test"];
11+
var sourcegraph = imports.sourcegraph;
12+
13+
var describe = test.describe;
14+
var it = test.it;
15+
var before = test.before;
16+
var after = test.after;
17+
var beforeEach = test.beforeEach;
18+
var afterEach = test.afterEach;
19+
var assert = test.assert;
20+
var expect = test.expect;
21+
22+
/***** Initialization *****/
23+
24+
describe("The module", function(){
25+
this.timeout(2000);
26+
27+
beforeEach(function() {
28+
});
29+
30+
afterEach(function () {
31+
});
32+
33+
it("has a sync test", function() {
34+
});
35+
36+
it("has a async test", function(done) {
37+
done();
38+
});
39+
40+
it("has a failing test", function() {
41+
assert.equal(10, 11);
42+
});
43+
});
44+
45+
register(null, {});
46+
}
47+
});

0 commit comments

Comments
 (0)