AutoCompleteManager.coffee 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. define [
  2. "auto-complete/MenuView"
  3. "auto-complete/SuggestionManager"
  4. "ace/range"
  5. ], (MenuView, SuggestionManager) ->
  6. Range = require("ace/range").Range
  7. class AutoCompleteManager
  8. constructor: (@ide) ->
  9. @aceEditor = @ide.editor.aceEditor
  10. @menu = new MenuView()
  11. @menu.render(
  12. @getAceContentEl().css("font-family"),
  13. @getAceContentEl().css("font-size")
  14. )
  15. @ide.mainAreaManager.getAreaElement("editor").append(@menu.$el)
  16. @menu.on "click", (e, suggestion) => @insertSuggestion(suggestion)
  17. @menuVisible = false
  18. @suggestionManager = new SuggestionManager()
  19. @bindToEditorEvents()
  20. @bindToAceInputEvents()
  21. bindToEditorEvents: () ->
  22. @ide.editor.on "change:doc", (@aceSession) =>
  23. @refreshSuggestionList()
  24. @aceSession.on "change", (change) => @onChange(change)
  25. @ide.editor.on "scroll", () =>
  26. @hideMenu()
  27. bindToAceInputEvents: () ->
  28. @oldOnCommandKey = @aceEditor.keyBinding.onCommandKey
  29. @aceEditor.keyBinding.onCommandKey = () => @onKeyPress.apply(@, arguments)
  30. $(@aceEditor.renderer.getContainerElement()).on "click", (e) => @onClick(e)
  31. onChange: (change) ->
  32. @scheduleSuggestionListRefresh()
  33. cursorPosition = @aceEditor.getCursorPosition()
  34. end = change.data.range.end
  35. # Check that this change was made by us, not a collaborator
  36. # (Cursor is still one place behind)
  37. if end.row == cursorPosition.row and end.column == cursorPosition.column + 1
  38. if change.data.action == "insertText"
  39. range = new Range(end.row, 0, end.row, end.column)
  40. lineUpToCursor = @aceSession.getTextRange(range)
  41. commandFragment = @getLastCommandFragment(lineUpToCursor)
  42. if commandFragment
  43. suggestions = @suggestionManager.getSuggestions(commandFragment)
  44. if suggestions.length > 0
  45. @positionMenu(commandFragment.length)
  46. @menu.setSuggestions suggestions
  47. @showMenu()
  48. else
  49. @hideMenu()
  50. else
  51. @hideMenu()
  52. else
  53. @hideMenu()
  54. onKeyPress: (e) ->
  55. keyCode = e.keyCode
  56. args = arguments
  57. delegate = () =>
  58. @oldOnCommandKey.apply(@aceEditor.keyBinding, args)
  59. if @menuVisible
  60. switch keyCode
  61. when @keyCodes.UP
  62. @menu.moveSelectionUp()
  63. when @keyCodes.DOWN
  64. @menu.moveSelectionDown()
  65. when @keyCodes.ENTER, @keyCodes.TAB
  66. @insertSuggestion(@menu.getSelectedSuggestion())
  67. e.preventDefault()
  68. @hideMenu()
  69. when @keyCodes.ESCAPE
  70. @hideMenu()
  71. else
  72. delegate()
  73. else
  74. delegate()
  75. positionMenu: (characterOffset) ->
  76. characterWidth = @getAceRenderer().characterWidth
  77. lineHeight = @getAceRenderer().lineHeight
  78. pos = @getCursorOffset()
  79. pos.top = pos.top + lineHeight
  80. styleOffset = 10 # CSS borders and margins
  81. pos.left = pos.left - styleOffset - characterOffset * characterWidth
  82. # We need to position the menu with coordinates relative to the
  83. # editor area.
  84. editorAreaOffset = @ide.mainAreaManager.getAreaElement("editor").offset()
  85. aceOffset = @getAceContentEl().offset()
  86. @menu.position
  87. top: aceOffset.top - editorAreaOffset.top + pos.top
  88. left: aceOffset.left - editorAreaOffset.left + pos.left
  89. insertSuggestion: (suggestion) ->
  90. if suggestion?
  91. oldCursorPosition = @aceEditor.getCursorPosition()
  92. @aceEditor.insert(suggestion.completion)
  93. @aceEditor.moveCursorTo(
  94. oldCursorPosition.row,
  95. oldCursorPosition.column + suggestion.completionBeforeCursor.length
  96. )
  97. @hideMenu()
  98. @aceEditor.focus()
  99. scheduleSuggestionListRefresh: () ->
  100. clearTimeout(@updateTimeoutId) if @updateTimeoutId?
  101. @updateTimeoutId = setTimeout((() =>
  102. @refreshSuggestionList()
  103. delete @updateTimeoutId
  104. ), 5000)
  105. refreshSuggestionList: () ->
  106. @suggestionManager.loadCommandsFromDoc(@aceSession.doc.getAllLines().join("\n"))
  107. onClick: () ->
  108. @hideMenu()
  109. getLastCommandFragment: (line) ->
  110. if m = line.match(/\\([^\\ ]+)$/)
  111. m[1]
  112. else
  113. null
  114. showMenu: () ->
  115. @menu.show()
  116. @menuVisible = true
  117. hideMenu: () ->
  118. @menu.hide()
  119. @menuVisible = false
  120. keyCodes: "UP": 38, "DOWN": 40, "ENTER": 13, "TAB": 9, "ESCAPE": 27
  121. getCursorOffset: () ->
  122. # This is fragile and relies on the internal Ace API not changing.
  123. # See $moveTextAreaToCursor in
  124. # https://github.com/ajaxorg/ace/blob/master/lib/ace/virtual_renderer.js
  125. @aceEditor.renderer.$cursorLayer.$pixelPos
  126. getAceRenderer: () -> @aceEditor.renderer
  127. getAceContentEl: () -> $(@aceEditor.renderer.getContainerElement()).find(".ace_content")