tern.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Glue code between CodeMirror and Tern.
  4. //
  5. // Create a CodeMirror.TernServer to wrap an actual Tern server,
  6. // register open documents (CodeMirror.Doc instances) with it, and
  7. // call its methods to activate the assisting functions that Tern
  8. // provides.
  9. //
  10. // Options supported (all optional):
  11. // * defs: An array of JSON definition data structures.
  12. // * plugins: An object mapping plugin names to configuration
  13. // options.
  14. // * getFile: A function(name, c) that can be used to access files in
  15. // the project that haven't been loaded yet. Simply do c(null) to
  16. // indicate that a file is not available.
  17. // * fileFilter: A function(value, docName, doc) that will be applied
  18. // to documents before passing them on to Tern.
  19. // * switchToDoc: A function(name, doc) that should, when providing a
  20. // multi-file view, switch the view or focus to the named file.
  21. // * showError: A function(editor, message) that can be used to
  22. // override the way errors are displayed.
  23. // * completionTip: Customize the content in tooltips for completions.
  24. // Is passed a single argument—the completion's data as returned by
  25. // Tern—and may return a string, DOM node, or null to indicate that
  26. // no tip should be shown. By default the docstring is shown.
  27. // * typeTip: Like completionTip, but for the tooltips shown for type
  28. // queries.
  29. // * responseFilter: A function(doc, query, request, error, data) that
  30. // will be applied to the Tern responses before treating them
  31. //
  32. //
  33. // It is possible to run the Tern server in a web worker by specifying
  34. // these additional options:
  35. // * useWorker: Set to true to enable web worker mode. You'll probably
  36. // want to feature detect the actual value you use here, for example
  37. // !!window.Worker.
  38. // * workerScript: The main script of the worker. Point this to
  39. // wherever you are hosting worker.js from this directory.
  40. // * workerDeps: An array of paths pointing (relative to workerScript)
  41. // to the Acorn and Tern libraries and any Tern plugins you want to
  42. // load. Or, if you minified those into a single script and included
  43. // them in the workerScript, simply leave this undefined.
  44. ;(function (mod) {
  45. if (typeof exports == 'object' && typeof module == 'object')
  46. // CommonJS
  47. mod(require('../../lib/codemirror'))
  48. else if (typeof define == 'function' && define.amd)
  49. // AMD
  50. define(['../../lib/codemirror'], mod)
  51. // Plain browser env
  52. else mod(CodeMirror)
  53. })(function (CodeMirror) {
  54. 'use strict'
  55. // declare global: tern
  56. CodeMirror.TernServer = function (options) {
  57. var self = this
  58. this.options = options || {}
  59. var plugins = this.options.plugins || (this.options.plugins = {})
  60. if (!plugins.doc_comment) plugins.doc_comment = true
  61. this.docs = Object.create(null)
  62. if (this.options.useWorker) {
  63. this.server = new WorkerServer(this)
  64. } else {
  65. this.server = new tern.Server({
  66. getFile: function (name, c) {
  67. return getFile(self, name, c)
  68. },
  69. async: true,
  70. defs: this.options.defs || [],
  71. plugins: plugins,
  72. })
  73. }
  74. this.trackChange = function (doc, change) {
  75. trackChange(self, doc, change)
  76. }
  77. this.cachedArgHints = null
  78. this.activeArgHints = null
  79. this.jumpStack = []
  80. this.getHint = function (cm, c) {
  81. return hint(self, cm, c)
  82. }
  83. this.getHint.async = true
  84. }
  85. CodeMirror.TernServer.prototype = {
  86. addDoc: function (name, doc) {
  87. var data = { doc: doc, name: name, changed: null }
  88. this.server.addFile(name, docValue(this, data))
  89. CodeMirror.on(doc, 'change', this.trackChange)
  90. return (this.docs[name] = data)
  91. },
  92. delDoc: function (id) {
  93. var found = resolveDoc(this, id)
  94. if (!found) return
  95. CodeMirror.off(found.doc, 'change', this.trackChange)
  96. delete this.docs[found.name]
  97. this.server.delFile(found.name)
  98. },
  99. hideDoc: function (id) {
  100. closeArgHints(this)
  101. var found = resolveDoc(this, id)
  102. if (found && found.changed) sendDoc(this, found)
  103. },
  104. complete: function (cm) {
  105. cm.showHint({ hint: this.getHint })
  106. },
  107. showType: function (cm, pos, c) {
  108. showContextInfo(this, cm, pos, 'type', c)
  109. },
  110. showDocs: function (cm, pos, c) {
  111. showContextInfo(this, cm, pos, 'documentation', c)
  112. },
  113. updateArgHints: function (cm) {
  114. updateArgHints(this, cm)
  115. },
  116. jumpToDef: function (cm) {
  117. jumpToDef(this, cm)
  118. },
  119. jumpBack: function (cm) {
  120. jumpBack(this, cm)
  121. },
  122. rename: function (cm) {
  123. rename(this, cm)
  124. },
  125. selectName: function (cm) {
  126. selectName(this, cm)
  127. },
  128. request: function (cm, query, c, pos) {
  129. var self = this
  130. var doc = findDoc(this, cm.getDoc())
  131. var request = buildRequest(this, doc, query, pos)
  132. var extraOptions = request.query && this.options.queryOptions && this.options.queryOptions[request.query.type]
  133. if (extraOptions) for (var prop in extraOptions) request.query[prop] = extraOptions[prop]
  134. this.server.request(request, function (error, data) {
  135. if (!error && self.options.responseFilter) data = self.options.responseFilter(doc, query, request, error, data)
  136. c(error, data)
  137. })
  138. },
  139. destroy: function () {
  140. closeArgHints(this)
  141. if (this.worker) {
  142. this.worker.terminate()
  143. this.worker = null
  144. }
  145. },
  146. }
  147. var Pos = CodeMirror.Pos
  148. var cls = 'CodeMirror-Tern-'
  149. var bigDoc = 250
  150. function getFile(ts, name, c) {
  151. var buf = ts.docs[name]
  152. if (buf) c(docValue(ts, buf))
  153. else if (ts.options.getFile) ts.options.getFile(name, c)
  154. else c(null)
  155. }
  156. function findDoc(ts, doc, name) {
  157. for (var n in ts.docs) {
  158. var cur = ts.docs[n]
  159. if (cur.doc == doc) return cur
  160. }
  161. if (!name)
  162. for (var i = 0; ; ++i) {
  163. n = '[doc' + (i || '') + ']'
  164. if (!ts.docs[n]) {
  165. name = n
  166. break
  167. }
  168. }
  169. return ts.addDoc(name, doc)
  170. }
  171. function resolveDoc(ts, id) {
  172. if (typeof id == 'string') return ts.docs[id]
  173. if (id instanceof CodeMirror) id = id.getDoc()
  174. if (id instanceof CodeMirror.Doc) return findDoc(ts, id)
  175. }
  176. function trackChange(ts, doc, change) {
  177. var data = findDoc(ts, doc)
  178. var argHints = ts.cachedArgHints
  179. if (argHints && argHints.doc == doc && cmpPos(argHints.start, change.to) >= 0) ts.cachedArgHints = null
  180. var changed = data.changed
  181. if (changed == null) data.changed = changed = { from: change.from.line, to: change.from.line }
  182. var end = change.from.line + (change.text.length - 1)
  183. if (change.from.line < changed.to) changed.to = changed.to - (change.to.line - end)
  184. if (end >= changed.to) changed.to = end + 1
  185. if (changed.from > change.from.line) changed.from = change.from.line
  186. if (doc.lineCount() > bigDoc && change.to - changed.from > 100)
  187. setTimeout(function () {
  188. if (data.changed && data.changed.to - data.changed.from > 100) sendDoc(ts, data)
  189. }, 200)
  190. }
  191. function sendDoc(ts, doc) {
  192. ts.server.request({ files: [{ type: 'full', name: doc.name, text: docValue(ts, doc) }] }, function (error) {
  193. if (error) window.console.error(error)
  194. else doc.changed = null
  195. })
  196. }
  197. // Completion
  198. function hint(ts, cm, c) {
  199. ts.request(cm, { type: 'completions', types: true, docs: true, urls: true }, function (error, data) {
  200. if (error) return showError(ts, cm, error)
  201. var completions = [],
  202. after = ''
  203. var from = data.start,
  204. to = data.end
  205. if (cm.getRange(Pos(from.line, from.ch - 2), from) == '["' && cm.getRange(to, Pos(to.line, to.ch + 2)) != '"]') after = '"]'
  206. for (var i = 0; i < data.completions.length; ++i) {
  207. var completion = data.completions[i],
  208. className = typeToIcon(completion.type)
  209. if (data.guess) className += ' ' + cls + 'guess'
  210. completions.push({ text: completion.name + after, displayText: completion.displayName || completion.name, className: className, data: completion })
  211. }
  212. var obj = { from: from, to: to, list: completions }
  213. var tooltip = null
  214. CodeMirror.on(obj, 'close', function () {
  215. remove(tooltip)
  216. })
  217. CodeMirror.on(obj, 'update', function () {
  218. remove(tooltip)
  219. })
  220. CodeMirror.on(obj, 'select', function (cur, node) {
  221. remove(tooltip)
  222. var content = ts.options.completionTip ? ts.options.completionTip(cur.data) : cur.data.doc
  223. if (content) {
  224. tooltip = makeTooltip(node.parentNode.getBoundingClientRect().right + window.pageXOffset, node.getBoundingClientRect().top + window.pageYOffset, content, cm, cls + 'hint-doc')
  225. }
  226. })
  227. c(obj)
  228. })
  229. }
  230. function typeToIcon(type) {
  231. var suffix
  232. if (type == '?') suffix = 'unknown'
  233. else if (type == 'number' || type == 'string' || type == 'bool') suffix = type
  234. else if (/^fn\(/.test(type)) suffix = 'fn'
  235. else if (/^\[/.test(type)) suffix = 'array'
  236. else suffix = 'object'
  237. return cls + 'completion ' + cls + 'completion-' + suffix
  238. }
  239. // Type queries
  240. function showContextInfo(ts, cm, pos, queryName, c) {
  241. ts.request(
  242. cm,
  243. queryName,
  244. function (error, data) {
  245. if (error) return showError(ts, cm, error)
  246. if (ts.options.typeTip) {
  247. var tip = ts.options.typeTip(data)
  248. } else {
  249. var tip = elt('span', null, elt('strong', null, data.type || 'not found'))
  250. if (data.doc) tip.appendChild(document.createTextNode(' — ' + data.doc))
  251. if (data.url) {
  252. tip.appendChild(document.createTextNode(' '))
  253. var child = tip.appendChild(elt('a', null, '[docs]'))
  254. child.href = data.url
  255. child.target = '_blank'
  256. }
  257. }
  258. tempTooltip(cm, tip, ts)
  259. if (c) c()
  260. },
  261. pos
  262. )
  263. }
  264. // Maintaining argument hints
  265. function updateArgHints(ts, cm) {
  266. closeArgHints(ts)
  267. if (cm.somethingSelected()) return
  268. var state = cm.getTokenAt(cm.getCursor()).state
  269. var inner = CodeMirror.innerMode(cm.getMode(), state)
  270. if (inner.mode.name != 'javascript') return
  271. var lex = inner.state.lexical
  272. if (lex.info != 'call') return
  273. var ch,
  274. argPos = lex.pos || 0,
  275. tabSize = cm.getOption('tabSize')
  276. for (var line = cm.getCursor().line, e = Math.max(0, line - 9), found = false; line >= e; --line) {
  277. var str = cm.getLine(line),
  278. extra = 0
  279. for (var pos = 0; ; ) {
  280. var tab = str.indexOf('\t', pos)
  281. if (tab == -1) break
  282. extra += tabSize - ((tab + extra) % tabSize) - 1
  283. pos = tab + 1
  284. }
  285. ch = lex.column - extra
  286. if (str.charAt(ch) == '(') {
  287. found = true
  288. break
  289. }
  290. }
  291. if (!found) return
  292. var start = Pos(line, ch)
  293. var cache = ts.cachedArgHints
  294. if (cache && cache.doc == cm.getDoc() && cmpPos(start, cache.start) == 0) return showArgHints(ts, cm, argPos)
  295. ts.request(cm, { type: 'type', preferFunction: true, end: start }, function (error, data) {
  296. if (error || !data.type || !/^fn\(/.test(data.type)) return
  297. ts.cachedArgHints = {
  298. start: start,
  299. type: parseFnType(data.type),
  300. name: data.exprName || data.name || 'fn',
  301. guess: data.guess,
  302. doc: cm.getDoc(),
  303. }
  304. showArgHints(ts, cm, argPos)
  305. })
  306. }
  307. function showArgHints(ts, cm, pos) {
  308. closeArgHints(ts)
  309. var cache = ts.cachedArgHints,
  310. tp = cache.type
  311. var tip = elt('span', cache.guess ? cls + 'fhint-guess' : null, elt('span', cls + 'fname', cache.name), '(')
  312. for (var i = 0; i < tp.args.length; ++i) {
  313. if (i) tip.appendChild(document.createTextNode(', '))
  314. var arg = tp.args[i]
  315. tip.appendChild(elt('span', cls + 'farg' + (i == pos ? ' ' + cls + 'farg-current' : ''), arg.name || '?'))
  316. if (arg.type != '?') {
  317. tip.appendChild(document.createTextNode(':\u00a0'))
  318. tip.appendChild(elt('span', cls + 'type', arg.type))
  319. }
  320. }
  321. tip.appendChild(document.createTextNode(tp.rettype ? ') ->\u00a0' : ')'))
  322. if (tp.rettype) tip.appendChild(elt('span', cls + 'type', tp.rettype))
  323. var place = cm.cursorCoords(null, 'page')
  324. var tooltip = (ts.activeArgHints = makeTooltip(place.right + 1, place.bottom, tip, cm))
  325. setTimeout(function () {
  326. tooltip.clear = onEditorActivity(cm, function () {
  327. if (ts.activeArgHints == tooltip) closeArgHints(ts)
  328. })
  329. }, 20)
  330. }
  331. function parseFnType(text) {
  332. var args = [],
  333. pos = 3
  334. function skipMatching(upto) {
  335. var depth = 0,
  336. start = pos
  337. for (;;) {
  338. var next = text.charAt(pos)
  339. if (upto.test(next) && !depth) return text.slice(start, pos)
  340. if (/[{\[\(]/.test(next)) ++depth
  341. else if (/[}\]\)]/.test(next)) --depth
  342. ++pos
  343. }
  344. }
  345. // Parse arguments
  346. if (text.charAt(pos) != ')')
  347. for (;;) {
  348. var name = text.slice(pos).match(/^([^, \(\[\{]+): /)
  349. if (name) {
  350. pos += name[0].length
  351. name = name[1]
  352. }
  353. args.push({ name: name, type: skipMatching(/[\),]/) })
  354. if (text.charAt(pos) == ')') break
  355. pos += 2
  356. }
  357. var rettype = text.slice(pos).match(/^\) -> (.*)$/)
  358. return { args: args, rettype: rettype && rettype[1] }
  359. }
  360. // Moving to the definition of something
  361. function jumpToDef(ts, cm) {
  362. function inner(varName) {
  363. var req = { type: 'definition', variable: varName || null }
  364. var doc = findDoc(ts, cm.getDoc())
  365. ts.server.request(buildRequest(ts, doc, req), function (error, data) {
  366. if (error) return showError(ts, cm, error)
  367. if (!data.file && data.url) {
  368. window.open(data.url)
  369. return
  370. }
  371. if (data.file) {
  372. var localDoc = ts.docs[data.file],
  373. found
  374. if (localDoc && (found = findContext(localDoc.doc, data))) {
  375. ts.jumpStack.push({ file: doc.name, start: cm.getCursor('from'), end: cm.getCursor('to') })
  376. moveTo(ts, doc, localDoc, found.start, found.end)
  377. return
  378. }
  379. }
  380. showError(ts, cm, 'Could not find a definition.')
  381. })
  382. }
  383. if (!atInterestingExpression(cm))
  384. dialog(cm, 'Jump to variable', function (name) {
  385. if (name) inner(name)
  386. })
  387. else inner()
  388. }
  389. function jumpBack(ts, cm) {
  390. var pos = ts.jumpStack.pop(),
  391. doc = pos && ts.docs[pos.file]
  392. if (!doc) return
  393. moveTo(ts, findDoc(ts, cm.getDoc()), doc, pos.start, pos.end)
  394. }
  395. function moveTo(ts, curDoc, doc, start, end) {
  396. doc.doc.setSelection(start, end)
  397. if (curDoc != doc && ts.options.switchToDoc) {
  398. closeArgHints(ts)
  399. ts.options.switchToDoc(doc.name, doc.doc)
  400. }
  401. }
  402. // The {line,ch} representation of positions makes this rather awkward.
  403. function findContext(doc, data) {
  404. var before = data.context.slice(0, data.contextOffset).split('\n')
  405. var startLine = data.start.line - (before.length - 1)
  406. var start = Pos(startLine, (before.length == 1 ? data.start.ch : doc.getLine(startLine).length) - before[0].length)
  407. var text = doc.getLine(startLine).slice(start.ch)
  408. for (var cur = startLine + 1; cur < doc.lineCount() && text.length < data.context.length; ++cur) text += '\n' + doc.getLine(cur)
  409. if (text.slice(0, data.context.length) == data.context) return data
  410. var cursor = doc.getSearchCursor(data.context, 0, false)
  411. var nearest,
  412. nearestDist = Infinity
  413. while (cursor.findNext()) {
  414. var from = cursor.from(),
  415. dist = Math.abs(from.line - start.line) * 10000
  416. if (!dist) dist = Math.abs(from.ch - start.ch)
  417. if (dist < nearestDist) {
  418. nearest = from
  419. nearestDist = dist
  420. }
  421. }
  422. if (!nearest) return null
  423. if (before.length == 1) nearest.ch += before[0].length
  424. else nearest = Pos(nearest.line + (before.length - 1), before[before.length - 1].length)
  425. if (data.start.line == data.end.line) var end = Pos(nearest.line, nearest.ch + (data.end.ch - data.start.ch))
  426. else var end = Pos(nearest.line + (data.end.line - data.start.line), data.end.ch)
  427. return { start: nearest, end: end }
  428. }
  429. function atInterestingExpression(cm) {
  430. var pos = cm.getCursor('end'),
  431. tok = cm.getTokenAt(pos)
  432. if (tok.start < pos.ch && tok.type == 'comment') return false
  433. return /[\w)\]]/.test(cm.getLine(pos.line).slice(Math.max(pos.ch - 1, 0), pos.ch + 1))
  434. }
  435. // Variable renaming
  436. function rename(ts, cm) {
  437. var token = cm.getTokenAt(cm.getCursor())
  438. if (!/\w/.test(token.string)) return showError(ts, cm, 'Not at a variable')
  439. dialog(cm, 'New name for ' + token.string, function (newName) {
  440. ts.request(cm, { type: 'rename', newName: newName, fullDocs: true }, function (error, data) {
  441. if (error) return showError(ts, cm, error)
  442. applyChanges(ts, data.changes)
  443. })
  444. })
  445. }
  446. function selectName(ts, cm) {
  447. var name = findDoc(ts, cm.doc).name
  448. ts.request(cm, { type: 'refs' }, function (error, data) {
  449. if (error) return showError(ts, cm, error)
  450. var ranges = [],
  451. cur = 0
  452. var curPos = cm.getCursor()
  453. for (var i = 0; i < data.refs.length; i++) {
  454. var ref = data.refs[i]
  455. if (ref.file == name) {
  456. ranges.push({ anchor: ref.start, head: ref.end })
  457. if (cmpPos(curPos, ref.start) >= 0 && cmpPos(curPos, ref.end) <= 0) cur = ranges.length - 1
  458. }
  459. }
  460. cm.setSelections(ranges, cur)
  461. })
  462. }
  463. var nextChangeOrig = 0
  464. function applyChanges(ts, changes) {
  465. var perFile = Object.create(null)
  466. for (var i = 0; i < changes.length; ++i) {
  467. var ch = changes[i]
  468. ;(perFile[ch.file] || (perFile[ch.file] = [])).push(ch)
  469. }
  470. for (var file in perFile) {
  471. var known = ts.docs[file],
  472. chs = perFile[file]
  473. if (!known) continue
  474. chs.sort(function (a, b) {
  475. return cmpPos(b.start, a.start)
  476. })
  477. var origin = '*rename' + ++nextChangeOrig
  478. for (var i = 0; i < chs.length; ++i) {
  479. var ch = chs[i]
  480. known.doc.replaceRange(ch.text, ch.start, ch.end, origin)
  481. }
  482. }
  483. }
  484. // Generic request-building helper
  485. function buildRequest(ts, doc, query, pos) {
  486. var files = [],
  487. offsetLines = 0,
  488. allowFragments = !query.fullDocs
  489. if (!allowFragments) delete query.fullDocs
  490. if (typeof query == 'string') query = { type: query }
  491. query.lineCharPositions = true
  492. if (query.end == null) {
  493. query.end = pos || doc.doc.getCursor('end')
  494. if (doc.doc.somethingSelected()) query.start = doc.doc.getCursor('start')
  495. }
  496. var startPos = query.start || query.end
  497. if (doc.changed) {
  498. if (doc.doc.lineCount() > bigDoc && allowFragments !== false && doc.changed.to - doc.changed.from < 100 && doc.changed.from <= startPos.line && doc.changed.to > query.end.line) {
  499. files.push(getFragmentAround(doc, startPos, query.end))
  500. query.file = '#0'
  501. var offsetLines = files[0].offsetLines
  502. if (query.start != null) query.start = Pos(query.start.line - -offsetLines, query.start.ch)
  503. query.end = Pos(query.end.line - offsetLines, query.end.ch)
  504. } else {
  505. files.push({ type: 'full', name: doc.name, text: docValue(ts, doc) })
  506. query.file = doc.name
  507. doc.changed = null
  508. }
  509. } else {
  510. query.file = doc.name
  511. }
  512. for (var name in ts.docs) {
  513. var cur = ts.docs[name]
  514. if (cur.changed && cur != doc) {
  515. files.push({ type: 'full', name: cur.name, text: docValue(ts, cur) })
  516. cur.changed = null
  517. }
  518. }
  519. return { query: query, files: files }
  520. }
  521. function getFragmentAround(data, start, end) {
  522. var doc = data.doc
  523. var minIndent = null,
  524. minLine = null,
  525. endLine,
  526. tabSize = 4
  527. for (var p = start.line - 1, min = Math.max(0, p - 50); p >= min; --p) {
  528. var line = doc.getLine(p),
  529. fn = line.search(/\bfunction\b/)
  530. if (fn < 0) continue
  531. var indent = CodeMirror.countColumn(line, null, tabSize)
  532. if (minIndent != null && minIndent <= indent) continue
  533. minIndent = indent
  534. minLine = p
  535. }
  536. if (minLine == null) minLine = min
  537. var max = Math.min(doc.lastLine(), end.line + 20)
  538. if (minIndent == null || minIndent == CodeMirror.countColumn(doc.getLine(start.line), null, tabSize)) endLine = max
  539. else
  540. for (endLine = end.line + 1; endLine < max; ++endLine) {
  541. var indent = CodeMirror.countColumn(doc.getLine(endLine), null, tabSize)
  542. if (indent <= minIndent) break
  543. }
  544. var from = Pos(minLine, 0)
  545. return { type: 'part', name: data.name, offsetLines: from.line, text: doc.getRange(from, Pos(endLine, end.line == endLine ? null : 0)) }
  546. }
  547. // Generic utilities
  548. var cmpPos = CodeMirror.cmpPos
  549. function elt(tagname, cls /*, ... elts*/) {
  550. var e = document.createElement(tagname)
  551. if (cls) e.className = cls
  552. for (var i = 2; i < arguments.length; ++i) {
  553. var elt = arguments[i]
  554. if (typeof elt == 'string') elt = document.createTextNode(elt)
  555. e.appendChild(elt)
  556. }
  557. return e
  558. }
  559. function dialog(cm, text, f) {
  560. if (cm.openDialog) cm.openDialog(text + ': <input type=text>', f)
  561. else f(prompt(text, ''))
  562. }
  563. // Tooltips
  564. function tempTooltip(cm, content, ts) {
  565. if (cm.state.ternTooltip) remove(cm.state.ternTooltip)
  566. var where = cm.cursorCoords()
  567. var tip = (cm.state.ternTooltip = makeTooltip(where.right + 1, where.bottom, content, cm))
  568. function maybeClear() {
  569. old = true
  570. if (!mouseOnTip) clear()
  571. }
  572. function clear() {
  573. cm.state.ternTooltip = null
  574. if (tip.parentNode) fadeOut(tip)
  575. clearActivity()
  576. }
  577. var mouseOnTip = false,
  578. old = false
  579. CodeMirror.on(tip, 'mousemove', function () {
  580. mouseOnTip = true
  581. })
  582. CodeMirror.on(tip, 'mouseout', function (e) {
  583. var related = e.relatedTarget || e.toElement
  584. if (!related || !CodeMirror.contains(tip, related)) {
  585. if (old) clear()
  586. else mouseOnTip = false
  587. }
  588. })
  589. setTimeout(maybeClear, ts.options.hintDelay ? ts.options.hintDelay : 1700)
  590. var clearActivity = onEditorActivity(cm, clear)
  591. }
  592. function onEditorActivity(cm, f) {
  593. cm.on('cursorActivity', f)
  594. cm.on('blur', f)
  595. cm.on('scroll', f)
  596. cm.on('setDoc', f)
  597. return function () {
  598. cm.off('cursorActivity', f)
  599. cm.off('blur', f)
  600. cm.off('scroll', f)
  601. cm.off('setDoc', f)
  602. }
  603. }
  604. function makeTooltip(x, y, content, cm, className) {
  605. var node = elt('div', cls + 'tooltip' + ' ' + (className || ''), content)
  606. node.style.left = x + 'px'
  607. node.style.top = y + 'px'
  608. var container = ((cm.options || {}).hintOptions || {}).container || document.body
  609. container.appendChild(node)
  610. var pos = cm.cursorCoords()
  611. var winW = window.innerWidth
  612. var winH = window.innerHeight
  613. var box = node.getBoundingClientRect()
  614. var hints = document.querySelector('.CodeMirror-hints')
  615. var overlapY = box.bottom - winH
  616. var overlapX = box.right - winW
  617. if (hints && overlapX > 0) {
  618. node.style.left = 0
  619. var box = node.getBoundingClientRect()
  620. node.style.left = (x = x - hints.offsetWidth - box.width) + 'px'
  621. overlapX = box.right - winW
  622. }
  623. if (overlapY > 0) {
  624. var height = box.bottom - box.top,
  625. curTop = pos.top - (pos.bottom - box.top)
  626. if (curTop - height > 0) {
  627. // Fits above cursor
  628. node.style.top = pos.top - height + 'px'
  629. } else if (height > winH) {
  630. node.style.height = winH - 5 + 'px'
  631. node.style.top = pos.bottom - box.top + 'px'
  632. }
  633. }
  634. if (overlapX > 0) {
  635. if (box.right - box.left > winW) {
  636. node.style.width = winW - 5 + 'px'
  637. overlapX -= box.right - box.left - winW
  638. }
  639. node.style.left = x - overlapX + 'px'
  640. }
  641. return node
  642. }
  643. function remove(node) {
  644. var p = node && node.parentNode
  645. if (p) p.removeChild(node)
  646. }
  647. function fadeOut(tooltip) {
  648. tooltip.style.opacity = '0'
  649. setTimeout(function () {
  650. remove(tooltip)
  651. }, 1100)
  652. }
  653. function showError(ts, cm, msg) {
  654. if (ts.options.showError) ts.options.showError(cm, msg)
  655. else tempTooltip(cm, String(msg), ts)
  656. }
  657. function closeArgHints(ts) {
  658. if (ts.activeArgHints) {
  659. if (ts.activeArgHints.clear) ts.activeArgHints.clear()
  660. remove(ts.activeArgHints)
  661. ts.activeArgHints = null
  662. }
  663. }
  664. function docValue(ts, doc) {
  665. var val = doc.doc.getValue()
  666. if (ts.options.fileFilter) val = ts.options.fileFilter(val, doc.name, doc.doc)
  667. return val
  668. }
  669. // Worker wrapper
  670. function WorkerServer(ts) {
  671. var worker = (ts.worker = new Worker(ts.options.workerScript))
  672. worker.postMessage({ type: 'init', defs: ts.options.defs, plugins: ts.options.plugins, scripts: ts.options.workerDeps })
  673. var msgId = 0,
  674. pending = {}
  675. function send(data, c) {
  676. if (c) {
  677. data.id = ++msgId
  678. pending[msgId] = c
  679. }
  680. worker.postMessage(data)
  681. }
  682. worker.onmessage = function (e) {
  683. var data = e.data
  684. if (data.type == 'getFile') {
  685. getFile(ts, data.name, function (err, text) {
  686. send({ type: 'getFile', err: String(err), text: text, id: data.id })
  687. })
  688. } else if (data.type == 'debug') {
  689. window.console.log(data.message)
  690. } else if (data.id && pending[data.id]) {
  691. pending[data.id](data.err, data.body)
  692. delete pending[data.id]
  693. }
  694. }
  695. worker.onerror = function (e) {
  696. for (var id in pending) pending[id](e)
  697. pending = {}
  698. }
  699. this.addFile = function (name, text) {
  700. send({ type: 'add', name: name, text: text })
  701. }
  702. this.delFile = function (name) {
  703. send({ type: 'del', name: name })
  704. }
  705. this.request = function (body, c) {
  706. send({ type: 'req', body: body }, c)
  707. }
  708. }
  709. })