TurboBadgerLanguageExtension.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. private active = false;
  30. /**
  31. * Initialize the language service
  32. * @param {Editor.ClientExtensions.ClientServiceLocator} serviceLocator
  33. */
  34. initialize(serviceLocator: Editor.ClientExtensions.ClientServiceLocator) {
  35. // initialize the language service
  36. this.serviceLocator = serviceLocator;
  37. serviceLocator.clientServices.register(this);
  38. }
  39. /**
  40. * Determines if the file name/path provided is something we care about
  41. * @param {string} path
  42. * @return {boolean}
  43. */
  44. private isValidFiletype(path: string): boolean {
  45. return path.toLowerCase().endsWith(".tb.txt") || path.toLowerCase().endsWith(".tb");
  46. }
  47. /**
  48. * Called when the editor needs to be configured for a particular file
  49. * @param {Editor.ClientExtensions.EditorFileEvent} ev
  50. */
  51. configureEditor(ev: Editor.ClientExtensions.EditorFileEvent) {
  52. if (this.isValidFiletype(ev.filename)) {
  53. this.active = true;
  54. monaco.languages.register({ id: "turbobadger" });
  55. // TODO: set up syntax hilighter
  56. // monaco.languages.setMonarchTokensProvider("turbobadger", this.getTokensProvider());
  57. let editor = <monaco.editor.IStandaloneCodeEditor>ev.editor;
  58. monaco.editor.setModelLanguage(editor.getModel(), "turbobadger");
  59. editor.updateOptions({
  60. renderWhitespace: "all",
  61. useTabStops: true,
  62. });
  63. }
  64. }
  65. /**
  66. * Called when code is first loaded into the editor
  67. * @param {CodeLoadedEvent} ev
  68. */
  69. codeLoaded(ev: Editor.ClientExtensions.CodeLoadedEvent) {
  70. if (this.isValidFiletype(ev.filename)) {
  71. let editor = <monaco.editor.IStandaloneCodeEditor>ev.editor;
  72. editor.getModel().updateOptions({
  73. insertSpaces: false
  74. });
  75. }
  76. }
  77. /**
  78. * Format the code
  79. */
  80. formatCode() {
  81. if (this.active) {
  82. alert("Code formatted not available for this syntax.");
  83. }
  84. }
  85. }