SuggestionManager.coffee 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. define [], () ->
  2. class Parser
  3. constructor: (@doc) ->
  4. parse: () ->
  5. commands = []
  6. seen = {}
  7. while command = @nextCommand()
  8. docState = @doc
  9. optionalArgs = 0
  10. while @consumeArgument("[", "]")
  11. optionalArgs++
  12. args = 0
  13. while @consumeArgument("{", "}")
  14. args++
  15. commandHash = "#{command}\\#{optionalArgs}\\#{args}"
  16. if !seen[commandHash]?
  17. seen[commandHash] = true
  18. commands.push [command, optionalArgs, args]
  19. # Reset to before argument to handle nested commands
  20. @doc = docState
  21. return commands
  22. # Ignore single letter commands since auto complete is moot then.
  23. commandRegex: /\\([a-zA-Z][a-zA-Z]+)/
  24. nextCommand: () ->
  25. i = @doc.search(@commandRegex)
  26. if i == -1
  27. return false
  28. else
  29. match = @doc.match(@commandRegex)[1]
  30. @doc = @doc.substr(i + match.length + 1)
  31. return match
  32. consumeWhitespace: () ->
  33. match = @doc.match(/^[ \t\n]*/m)[0]
  34. @doc = @doc.substr(match.length)
  35. consumeArgument: (openingBracket, closingBracket) ->
  36. @consumeWhitespace()
  37. if @doc[0] == openingBracket
  38. i = 1
  39. bracketParity = 1
  40. while bracketParity > 0 and i < @doc.length
  41. if @doc[i] == openingBracket
  42. bracketParity++
  43. else if @doc[i] == closingBracket
  44. bracketParity--
  45. i++
  46. if bracketParity == 0
  47. @doc = @doc.substr(i)
  48. return true
  49. else
  50. return false
  51. else
  52. return false
  53. class SuggestionManager
  54. getCompletions: (editor, session, pos, prefix, callback) ->
  55. doc = session.getValue()
  56. parser = new Parser(doc)
  57. commands = parser.parse()
  58. completions = []
  59. for command in commands
  60. caption = "\\#{command[0]}"
  61. snippet = caption
  62. i = 1
  63. _.times command[1], () ->
  64. snippet += "[${#{i}}]"
  65. caption += "[]"
  66. i++
  67. _.times command[2], () ->
  68. snippet += "{${#{i}}}"
  69. caption += "{}"
  70. i++
  71. unless caption == prefix
  72. completions.push {
  73. caption: caption
  74. snippet: snippet
  75. meta: "cmd"
  76. }
  77. callback null, completions
  78. loadCommandsFromDoc: (doc) ->
  79. parser = new Parser(doc)
  80. @commands = parser.parse()
  81. getSuggestions: (commandFragment) ->
  82. matchingCommands = _.filter @commands, (command) ->
  83. command[0].slice(0, commandFragment.length) == commandFragment
  84. return _.map matchingCommands, (command) ->
  85. base = "\\" + commandFragment
  86. args = ""
  87. _.times command[1], () -> args = args + "[]"
  88. _.times command[2], () -> args = args + "{}"
  89. completionBase = command[0].slice(commandFragment.length)
  90. squareArgsNo = command[1]
  91. curlyArgsNo = command[2]
  92. totalArgs = squareArgsNo + curlyArgsNo
  93. if totalArgs == 0
  94. completionBeforeCursor = completionBase
  95. completionAfterCurspr = ""
  96. else
  97. completionBeforeCursor = completionBase + args[0]
  98. completionAfterCursor = args.slice(1)
  99. return {
  100. base: base,
  101. completion: completionBase + args,
  102. completionBeforeCursor: completionBeforeCursor
  103. completionAfterCursor: completionAfterCursor
  104. }