javascript-hint.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. ;(function (mod) {
  4. if (typeof exports == 'object' && typeof module == 'object')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. var Pos = CodeMirror.Pos
  14. function forEach(arr, f) {
  15. for (var i = 0, e = arr.length; i < e; ++i) f(arr[i])
  16. }
  17. function arrayContains(arr, item) {
  18. if (!Array.prototype.indexOf) {
  19. var i = arr.length
  20. while (i--) {
  21. if (arr[i] === item) {
  22. return true
  23. }
  24. }
  25. return false
  26. }
  27. return arr.indexOf(item) != -1
  28. }
  29. function scriptHint(editor, keywords, getToken, options) {
  30. // Find the token at the cursor
  31. var cur = editor.getCursor(),
  32. token = getToken(editor, cur)
  33. if (/\b(?:string|comment)\b/.test(token.type)) return
  34. var innerMode = CodeMirror.innerMode(editor.getMode(), token.state)
  35. if (innerMode.mode.helperType === 'json') return
  36. token.state = innerMode.state
  37. // If it's not a 'word-style' token, ignore the token.
  38. if (!/^[\w$_]*$/.test(token.string)) {
  39. token = { start: cur.ch, end: cur.ch, string: '', state: token.state, type: token.string == '.' ? 'property' : null }
  40. } else if (token.end > cur.ch) {
  41. token.end = cur.ch
  42. token.string = token.string.slice(0, cur.ch - token.start)
  43. }
  44. var tprop = token
  45. // If it is a property, find out what it is a property of.
  46. while (tprop.type == 'property') {
  47. tprop = getToken(editor, Pos(cur.line, tprop.start))
  48. if (tprop.string != '.') return
  49. tprop = getToken(editor, Pos(cur.line, tprop.start))
  50. if (!context) var context = []
  51. context.push(tprop)
  52. }
  53. return { list: getCompletions(token, context, keywords, options), from: Pos(cur.line, token.start), to: Pos(cur.line, token.end) }
  54. }
  55. function javascriptHint(editor, options) {
  56. return scriptHint(
  57. editor,
  58. javascriptKeywords,
  59. function (e, cur) {
  60. return e.getTokenAt(cur)
  61. },
  62. options
  63. )
  64. }
  65. CodeMirror.registerHelper('hint', 'javascript', javascriptHint)
  66. function getCoffeeScriptToken(editor, cur) {
  67. // This getToken, it is for coffeescript, imitates the behavior of
  68. // getTokenAt method in javascript.js, that is, returning "property"
  69. // type and treat "." as independent token.
  70. var token = editor.getTokenAt(cur)
  71. if (cur.ch == token.start + 1 && token.string.charAt(0) == '.') {
  72. token.end = token.start
  73. token.string = '.'
  74. token.type = 'property'
  75. } else if (/^\.[\w$_]*$/.test(token.string)) {
  76. token.type = 'property'
  77. token.start++
  78. token.string = token.string.replace(/\./, '')
  79. }
  80. return token
  81. }
  82. function coffeescriptHint(editor, options) {
  83. return scriptHint(editor, coffeescriptKeywords, getCoffeeScriptToken, options)
  84. }
  85. CodeMirror.registerHelper('hint', 'coffeescript', coffeescriptHint)
  86. var stringProps = ('charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight ' + 'toUpperCase toLowerCase split concat match replace search').split(' ')
  87. var arrayProps = ('length concat join splice push pop shift unshift slice reverse sort indexOf ' + 'lastIndexOf every some filter forEach map reduce reduceRight ').split(' ')
  88. var funcProps = 'prototype apply call bind'.split(' ')
  89. var javascriptKeywords = (
  90. 'break case catch class const continue debugger default delete do else export extends false finally for function ' +
  91. 'if in import instanceof new null return super switch this throw true try typeof var void while with yield'
  92. ).split(' ')
  93. var coffeescriptKeywords = (
  94. 'and break catch class continue delete do else extends false finally for ' +
  95. 'if in instanceof isnt new no not null of off on or return switch then throw true try typeof until void while with yes'
  96. ).split(' ')
  97. function forAllProps(obj, callback) {
  98. if (!Object.getOwnPropertyNames || !Object.getPrototypeOf) {
  99. for (var name in obj) callback(name)
  100. } else {
  101. for (var o = obj; o; o = Object.getPrototypeOf(o)) Object.getOwnPropertyNames(o).forEach(callback)
  102. }
  103. }
  104. function getCompletions(token, context, keywords, options) {
  105. var found = [],
  106. start = token.string,
  107. global = (options && options.globalScope) || window
  108. function maybeAdd(str) {
  109. if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str)
  110. }
  111. function gatherCompletions(obj) {
  112. if (typeof obj == 'string') forEach(stringProps, maybeAdd)
  113. else if (obj instanceof Array) forEach(arrayProps, maybeAdd)
  114. else if (obj instanceof Function) forEach(funcProps, maybeAdd)
  115. forAllProps(obj, maybeAdd)
  116. }
  117. if (context && context.length) {
  118. // If this is a property, see if it belongs to some object we can
  119. // find in the current environment.
  120. var obj = context.pop(),
  121. base
  122. if (obj.type && obj.type.indexOf('variable') === 0) {
  123. if (options && options.additionalContext) base = options.additionalContext[obj.string]
  124. if (!options || options.useGlobalScope !== false) base = base || global[obj.string]
  125. } else if (obj.type == 'string') {
  126. base = ''
  127. } else if (obj.type == 'atom') {
  128. base = 1
  129. } else if (obj.type == 'function') {
  130. if (global.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') && typeof global.jQuery == 'function') base = global.jQuery()
  131. else if (global._ != null && obj.string == '_' && typeof global._ == 'function') base = global._()
  132. }
  133. while (base != null && context.length) base = base[context.pop().string]
  134. if (base != null) gatherCompletions(base)
  135. } else {
  136. // If not, just look in the global object, any local scope, and optional additional-context
  137. // (reading into JS mode internals to get at the local and global variables)
  138. for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name)
  139. for (var c = token.state.context; c; c = c.prev) for (var v = c.vars; v; v = v.next) maybeAdd(v.name)
  140. for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name)
  141. if (options && options.additionalContext != null) for (var key in options.additionalContext) maybeAdd(key)
  142. if (!options || options.useGlobalScope !== false) gatherCompletions(global)
  143. forEach(keywords, maybeAdd)
  144. }
  145. return found
  146. }
  147. })