AutoCompleteManager.coffee 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. define [
  2. "auto-complete/SuggestionManager"
  3. "ace/autocomplete/util"
  4. "ace/range"
  5. "ace/ext/language_tools"
  6. ], (SuggestionManager, Util) ->
  7. Range = require("ace/range").Range
  8. Util.retrievePrecedingIdentifier = (text, pos, regex) ->
  9. currentLineOffset = 0
  10. for i in [(pos-1)..0]
  11. if text[i] == "\n"
  12. currentLineOffset = i + 1
  13. break
  14. currentLine = text.slice(currentLineOffset, pos)
  15. fragment = getLastCommandFragment(currentLine) or ""
  16. return fragment
  17. getLastCommandFragment = (lineUpToCursor) ->
  18. if m = lineUpToCursor.match(/(\\[^\\ ]+)$/)
  19. return m[1]
  20. else
  21. return null
  22. class AutoCompleteManager
  23. constructor: (@ide) ->
  24. @suggestionManager = new SuggestionManager()
  25. @aceEditor = @ide.editor.aceEditor
  26. @aceEditor.setOptions({
  27. enableBasicAutocompletion: true,
  28. enableSnippets: true
  29. })
  30. @aceEditor.completers = [@suggestionManager]
  31. @bindToEditorEvents()
  32. bindToEditorEvents: () ->
  33. @ide.editor.on "change:doc", (@aceSession) =>
  34. @aceSession.on "change", (change) => @onChange(change)
  35. onChange: (change) ->
  36. cursorPosition = @aceEditor.getCursorPosition()
  37. end = change.data.range.end
  38. # Check that this change was made by us, not a collaborator
  39. # (Cursor is still one place behind)
  40. if end.row == cursorPosition.row and end.column == cursorPosition.column + 1
  41. if change.data.action == "insertText"
  42. range = new Range(end.row, 0, end.row, end.column)
  43. lineUpToCursor = @aceSession.getTextRange(range)
  44. commandFragment = getLastCommandFragment(lineUpToCursor)
  45. if commandFragment?
  46. setTimeout () =>
  47. @aceEditor.execCommand("startAutocomplete")
  48. $(@aceEditor.completer.popup.container)
  49. .find(".ace_content")
  50. .css("font-size": window.userSettings.fontSize + "px")
  51. , 0