TurboBadgerLanguageExtension.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. /**
  23. * Resource extension that handles configuring the editor for Javascript
  24. */
  25. export default class TurboBadgerLanguageExtension implements Editor.ClientExtensions.WebViewServiceEventListener {
  26. name: string = "ClientTurboBadgerLanguageExtension";
  27. description: string = "TurboBadger language services for the editor.";
  28. private serviceLocator: Editor.ClientExtensions.ClientServiceLocator;
  29. /**
  30. * Initialize the language service
  31. * @param {Editor.ClientExtensions.ClientServiceLocator} serviceLocator
  32. */
  33. initialize(serviceLocator: Editor.ClientExtensions.ClientServiceLocator) {
  34. // initialize the language service
  35. this.serviceLocator = serviceLocator;
  36. serviceLocator.clientServices.register(this);
  37. }
  38. /**
  39. * Determines if the file name/path provided is something we care about
  40. * @param {string} path
  41. * @return {boolean}
  42. */
  43. private isValidFiletype(path: string): boolean {
  44. return path.toLowerCase().endsWith(".tb.txt") || path.toLowerCase().endsWith(".tb");
  45. }
  46. /**
  47. * Called when the editor needs to be configured for a particular file
  48. * @param {Editor.EditorEvents.EditorFileEvent} ev
  49. */
  50. configureEditor(ev: Editor.EditorEvents.EditorFileEvent) {
  51. if (this.isValidFiletype(ev.filename)) {
  52. monaco.languages.register({ id: "turbobadger" });
  53. // TODO: set up syntax hilighter
  54. // monaco.languages.setMonarchTokensProvider("turbobadger", this.getTokensProvider());
  55. let editor = <monaco.editor.IStandaloneCodeEditor>ev.editor;
  56. monaco.editor.setModelLanguage(editor.getModel(), "turbobadger");
  57. editor.updateOptions({
  58. renderWhitespace: true,
  59. useTabStops: true,
  60. });
  61. }
  62. }
  63. /**
  64. * Called when code is first loaded into the editor
  65. * @param {CodeLoadedEvent} ev
  66. */
  67. codeLoaded(ev: Editor.EditorEvents.CodeLoadedEvent) {
  68. if (this.isValidFiletype(ev.filename)) {
  69. let editor = <monaco.editor.IStandaloneCodeEditor>ev.editor;
  70. editor.getModel().updateOptions({
  71. insertSpaces: false
  72. });
  73. }
  74. }
  75. }