@difizen/libro-lsp 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +0 -0
  3. package/es/adapters/adapter.d.ts +255 -0
  4. package/es/adapters/adapter.d.ts.map +1 -0
  5. package/es/adapters/adapter.js +647 -0
  6. package/es/adapters/notebook-adapter.d.ts +150 -0
  7. package/es/adapters/notebook-adapter.d.ts.map +1 -0
  8. package/es/adapters/notebook-adapter.js +588 -0
  9. package/es/adapters/status-message.d.ts +48 -0
  10. package/es/adapters/status-message.d.ts.map +1 -0
  11. package/es/adapters/status-message.js +110 -0
  12. package/es/connection-manager.d.ts +199 -0
  13. package/es/connection-manager.d.ts.map +1 -0
  14. package/es/connection-manager.js +685 -0
  15. package/es/connection.d.ts +149 -0
  16. package/es/connection.d.ts.map +1 -0
  17. package/es/connection.js +591 -0
  18. package/es/extractors/index.d.ts +4 -0
  19. package/es/extractors/index.d.ts.map +1 -0
  20. package/es/extractors/index.js +6 -0
  21. package/es/extractors/manager.d.ts +31 -0
  22. package/es/extractors/manager.d.ts.map +1 -0
  23. package/es/extractors/manager.js +90 -0
  24. package/es/extractors/text-extractor.d.ts +56 -0
  25. package/es/extractors/text-extractor.d.ts.map +1 -0
  26. package/es/extractors/text-extractor.js +72 -0
  27. package/es/extractors/types.d.ts +68 -0
  28. package/es/extractors/types.d.ts.map +1 -0
  29. package/es/extractors/types.js +1 -0
  30. package/es/feature.d.ts +29 -0
  31. package/es/feature.d.ts.map +1 -0
  32. package/es/feature.js +85 -0
  33. package/es/index.d.ts +19 -0
  34. package/es/index.d.ts.map +1 -0
  35. package/es/index.js +21 -0
  36. package/es/lsp-app-contribution.d.ts +19 -0
  37. package/es/lsp-app-contribution.d.ts.map +1 -0
  38. package/es/lsp-app-contribution.js +155 -0
  39. package/es/lsp-protocol.d.ts +10 -0
  40. package/es/lsp-protocol.d.ts.map +1 -0
  41. package/es/lsp-protocol.js +1 -0
  42. package/es/lsp.d.ts +136 -0
  43. package/es/lsp.d.ts.map +1 -0
  44. package/es/lsp.js +141 -0
  45. package/es/manager.d.ts +142 -0
  46. package/es/manager.d.ts.map +1 -0
  47. package/es/manager.js +423 -0
  48. package/es/module.d.ts +3 -0
  49. package/es/module.d.ts.map +1 -0
  50. package/es/module.js +21 -0
  51. package/es/plugin.d.ts +56 -0
  52. package/es/plugin.d.ts.map +1 -0
  53. package/es/plugin.js +0 -0
  54. package/es/positioning.d.ts +66 -0
  55. package/es/positioning.d.ts.map +1 -0
  56. package/es/positioning.js +96 -0
  57. package/es/schema.d.ts +240 -0
  58. package/es/schema.d.ts.map +1 -0
  59. package/es/schema.js +0 -0
  60. package/es/tokens.d.ts +677 -0
  61. package/es/tokens.d.ts.map +1 -0
  62. package/es/tokens.js +183 -0
  63. package/es/utils.d.ts +33 -0
  64. package/es/utils.d.ts.map +1 -0
  65. package/es/utils.js +168 -0
  66. package/es/virtual/document.d.ts +546 -0
  67. package/es/virtual/document.d.ts.map +1 -0
  68. package/es/virtual/document.js +1263 -0
  69. package/es/ws-connection/server-capability-registration.d.ts +19 -0
  70. package/es/ws-connection/server-capability-registration.d.ts.map +1 -0
  71. package/es/ws-connection/server-capability-registration.js +51 -0
  72. package/es/ws-connection/types.d.ts +76 -0
  73. package/es/ws-connection/types.d.ts.map +1 -0
  74. package/es/ws-connection/types.js +1 -0
  75. package/es/ws-connection/ws-connection.d.ts +105 -0
  76. package/es/ws-connection/ws-connection.d.ts.map +1 -0
  77. package/es/ws-connection/ws-connection.js +301 -0
  78. package/package.json +67 -0
  79. package/src/adapters/adapter.ts +611 -0
  80. package/src/adapters/notebook-adapter.ts +463 -0
  81. package/src/adapters/status-message.ts +93 -0
  82. package/src/connection-manager.ts +626 -0
  83. package/src/connection.ts +570 -0
  84. package/src/extractors/index.ts +6 -0
  85. package/src/extractors/manager.ts +82 -0
  86. package/src/extractors/text-extractor.ts +94 -0
  87. package/src/extractors/types.ts +78 -0
  88. package/src/feature.ts +60 -0
  89. package/src/index.spec.ts +10 -0
  90. package/src/index.ts +21 -0
  91. package/src/lsp-app-contribution.ts +83 -0
  92. package/src/lsp-protocol.ts +10 -0
  93. package/src/lsp.ts +160 -0
  94. package/src/manager.ts +358 -0
  95. package/src/module.ts +32 -0
  96. package/src/plugin.ts +62 -0
  97. package/src/positioning.ts +121 -0
  98. package/src/schema.ts +249 -0
  99. package/src/tokens.ts +843 -0
  100. package/src/utils.ts +109 -0
  101. package/src/virtual/document.ts +1250 -0
  102. package/src/ws-connection/server-capability-registration.ts +77 -0
  103. package/src/ws-connection/types.ts +102 -0
  104. package/src/ws-connection/ws-connection.ts +320 -0
@@ -0,0 +1,647 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
3
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
5
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
6
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
7
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
8
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
9
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
10
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
11
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
12
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
13
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
14
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
15
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
16
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
17
+ // Copyright (c) Jupyter Development Team.
18
+ // Distributed under the terms of the Modified BSD License.
19
+
20
+ import { Emitter } from '@difizen/mana-app';
21
+ import mergeWith from 'lodash.mergewith';
22
+ /**
23
+ * The values should follow the https://microsoft.github.io/language-server-protocol/specification guidelines
24
+ */
25
+ var MIME_TYPE_LANGUAGE_MAP = {
26
+ 'text/x-rsrc': 'r',
27
+ 'text/x-r-source': 'r',
28
+ // currently there are no LSP servers for IPython we are aware of
29
+ 'text/x-ipython': 'python'
30
+ };
31
+ /**
32
+ * Foreign code: low level adapter is not aware of the presence of foreign languages;
33
+ * it operates on the virtual document and must not attempt to infer the language dependencies
34
+ * as this would make the logic of inspections caching impossible to maintain, thus the WidgetAdapter
35
+ * has to handle that, keeping multiple connections and multiple virtual documents.
36
+ */
37
+ export var WidgetLSPAdapter = /*#__PURE__*/function () {
38
+ // note: it could be using namespace/IOptions pattern,
39
+ // but I do not know how to make it work with the generic type T
40
+ // (other than using 'any' in the IOptions interface)
41
+ function WidgetLSPAdapter(widget, options) {
42
+ var _this = this;
43
+ _classCallCheck(this, WidgetLSPAdapter);
44
+ this.widget = widget;
45
+ this.options = options;
46
+ /**
47
+ * Callback on document saved event.
48
+ */
49
+ this.onSaveState = function () {
50
+ // ignore premature calls (before the editor was initialized)
51
+ if (_this.virtualDocument === null) {
52
+ return;
53
+ }
54
+ var documentsToSave = [_this.virtualDocument];
55
+ for (var _i = 0, _documentsToSave = documentsToSave; _i < _documentsToSave.length; _i++) {
56
+ var virtualDocument = _documentsToSave[_i];
57
+ var connection = _this.connectionManager.connections.get(virtualDocument.uri);
58
+ if (!connection) {
59
+ continue;
60
+ }
61
+ connection.sendSaved(virtualDocument.documentInfo);
62
+ var _iterator = _createForOfIteratorHelper(virtualDocument.foreignDocuments.values()),
63
+ _step;
64
+ try {
65
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
66
+ var foreign = _step.value;
67
+ documentsToSave.push(foreign);
68
+ }
69
+ } catch (err) {
70
+ _iterator.e(err);
71
+ } finally {
72
+ _iterator.f();
73
+ }
74
+ }
75
+ };
76
+ /**
77
+ * Signal emitted when the adapter is connected.
78
+ */
79
+ this._adapterConnected = new Emitter();
80
+ /**
81
+ * Signal emitted when the active editor have changed.
82
+ */
83
+ this._activeEditorChanged = new Emitter();
84
+ /**
85
+ * Signal emitted when an editor is changed.
86
+ */
87
+ this._editorAdded = new Emitter();
88
+ /**
89
+ * Signal emitted when an editor is removed.
90
+ */
91
+ this._editorRemoved = new Emitter();
92
+ /**
93
+ * Signal emitted when the adapter is disposed.
94
+ */
95
+ this._disposed = new Emitter();
96
+ this._isDisposed = false;
97
+ this._virtualDocument = null;
98
+ this._connectionManager = options.connectionManager;
99
+ this._isConnected = false;
100
+ // set up signal connections
101
+ this.widget.onSave(this.onSaveState);
102
+ // this.widget.context.saveState.connect(this.onSaveState, this);
103
+ // this.connectionManager.closed.connect(this.onConnectionClosed, this);
104
+ // this.widget.disposed.connect(this.dispose, this);
105
+ }
106
+
107
+ /**
108
+ * Check if the adapter is disposed
109
+ */
110
+ _createClass(WidgetLSPAdapter, [{
111
+ key: "disposed",
112
+ get: function get() {
113
+ return this._isDisposed;
114
+ }
115
+ /**
116
+ * Check if the document contains multiple editors
117
+ */
118
+ }, {
119
+ key: "hasMultipleEditors",
120
+ get: function get() {
121
+ return this.editors.length > 1;
122
+ }
123
+ /**
124
+ * Get the ID of the internal widget.
125
+ */
126
+ }, {
127
+ key: "widgetId",
128
+ get: function get() {
129
+ return this.widget.id;
130
+ }
131
+
132
+ /**
133
+ * Get the language identifier of the document
134
+ */
135
+ }, {
136
+ key: "language",
137
+ get: function get() {
138
+ // the values should follow https://microsoft.github.io/language-server-protocol/specification guidelines,
139
+ // see the table in https://microsoft.github.io/language-server-protocol/specification#textDocumentItem
140
+ if (Object.prototype.hasOwnProperty.call(MIME_TYPE_LANGUAGE_MAP, this.mimeType)) {
141
+ return MIME_TYPE_LANGUAGE_MAP[this.mimeType];
142
+ } else {
143
+ var withoutParameters = this.mimeType.split(';')[0];
144
+ var _withoutParameters$sp = withoutParameters.split('/'),
145
+ _withoutParameters$sp2 = _slicedToArray(_withoutParameters$sp, 2),
146
+ type = _withoutParameters$sp2[0],
147
+ subtype = _withoutParameters$sp2[1];
148
+ if (type === 'application' || type === 'text') {
149
+ if (subtype.startsWith('x-')) {
150
+ return subtype.substring(2);
151
+ } else {
152
+ return subtype;
153
+ }
154
+ } else {
155
+ return this.mimeType;
156
+ }
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Signal emitted when the adapter is connected.
162
+ */
163
+ }, {
164
+ key: "adapterConnected",
165
+ get: function get() {
166
+ return this._adapterConnected.event;
167
+ }
168
+
169
+ /**
170
+ * Signal emitted when the active editor have changed.
171
+ */
172
+ }, {
173
+ key: "activeEditorChanged",
174
+ get: function get() {
175
+ return this._activeEditorChanged.event;
176
+ }
177
+
178
+ /**
179
+ * Signal emitted when the adapter is disposed.
180
+ */
181
+ }, {
182
+ key: "onDispose",
183
+ get: function get() {
184
+ return this._disposed.event;
185
+ }
186
+
187
+ /**
188
+ * Signal emitted when the an editor is changed.
189
+ */
190
+ }, {
191
+ key: "editorAdded",
192
+ get: function get() {
193
+ return this._editorAdded.event;
194
+ }
195
+
196
+ /**
197
+ * Signal emitted when the an editor is removed.
198
+ */
199
+ }, {
200
+ key: "editorRemoved",
201
+ get: function get() {
202
+ return this._editorRemoved.event;
203
+ }
204
+
205
+ /**
206
+ * Get the inner HTMLElement of the document widget.
207
+ */
208
+ }, {
209
+ key: "isConnected",
210
+ get:
211
+ /**
212
+ * The virtual document is connected or not
213
+ */
214
+ function get() {
215
+ return this._isConnected;
216
+ }
217
+
218
+ /**
219
+ * The LSP document and connection manager instance.
220
+ */
221
+ }, {
222
+ key: "connectionManager",
223
+ get: function get() {
224
+ return this._connectionManager;
225
+ }
226
+
227
+ /**
228
+ * Promise that resolves once the document is updated
229
+ */
230
+ }, {
231
+ key: "updateFinished",
232
+ get: function get() {
233
+ return this._updateFinished;
234
+ }
235
+
236
+ /**
237
+ * Internal virtual document of the adapter.
238
+ */
239
+ }, {
240
+ key: "virtualDocument",
241
+ get: function get() {
242
+ return this._virtualDocument;
243
+ }
244
+
245
+ /**
246
+ * Callback on connection closed event.
247
+ */
248
+ }, {
249
+ key: "onConnectionClosed",
250
+ value: function onConnectionClosed(_, _ref) {
251
+ var virtualDocument = _ref.virtualDocument;
252
+ if (virtualDocument === this.virtualDocument) {
253
+ this.dispose();
254
+ }
255
+ }
256
+
257
+ /**
258
+ * Dispose the adapter.
259
+ */
260
+ }, {
261
+ key: "dispose",
262
+ value: function dispose() {
263
+ if (this._isDisposed) {
264
+ return;
265
+ }
266
+ this._isDisposed = true;
267
+ this.disconnect();
268
+ this._virtualDocument = null;
269
+ this._disposed.fire();
270
+ }
271
+
272
+ /**
273
+ * Disconnect virtual document from the language server.
274
+ */
275
+ }, {
276
+ key: "disconnect",
277
+ value: function disconnect() {
278
+ var _this$virtualDocument, _this$virtualDocument2;
279
+ var uri = (_this$virtualDocument = this.virtualDocument) === null || _this$virtualDocument === void 0 ? void 0 : _this$virtualDocument.uri;
280
+ if (uri) {
281
+ this.connectionManager.unregisterDocument(uri);
282
+ }
283
+
284
+ // pretend that all editors were removed to trigger the disconnection of even handlers
285
+ // they will be connected again on new connection
286
+ var _iterator2 = _createForOfIteratorHelper(this.editors),
287
+ _step2;
288
+ try {
289
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
290
+ var editor = _step2.value.ceEditor;
291
+ this._editorRemoved.fire({
292
+ editor: editor
293
+ });
294
+ }
295
+ } catch (err) {
296
+ _iterator2.e(err);
297
+ } finally {
298
+ _iterator2.f();
299
+ }
300
+ (_this$virtualDocument2 = this.virtualDocument) === null || _this$virtualDocument2 === void 0 || _this$virtualDocument2.dispose();
301
+ }
302
+
303
+ /**
304
+ * Update the virtual document.
305
+ */
306
+ }, {
307
+ key: "updateDocuments",
308
+ value: function updateDocuments() {
309
+ if (this._isDisposed) {
310
+ console.warn('Cannot update documents: adapter disposed');
311
+ return Promise.reject('Cannot update documents: adapter disposed');
312
+ }
313
+ return this.virtualDocument.updateManager.updateDocuments(this.editors);
314
+ }
315
+
316
+ /**
317
+ * Callback called on the document changed event.
318
+ */
319
+ }, {
320
+ key: "documentChanged",
321
+ value: function documentChanged(virtualDocument) {
322
+ if (this._isDisposed) {
323
+ console.warn('Cannot swap document: adapter disposed');
324
+ return;
325
+ }
326
+
327
+ // TODO only send the difference, using connection.sendSelectiveChange()
328
+ var connection = this.connectionManager.connections.get(virtualDocument.uri);
329
+ if (!(connection !== null && connection !== void 0 && connection.isReady)) {
330
+ console.warn('Skipping document update signal: connection not ready');
331
+ return;
332
+ }
333
+ connection.sendFullTextChange(virtualDocument.value, virtualDocument.documentInfo);
334
+ }
335
+
336
+ /**
337
+ * (re)create virtual document using current path and language
338
+ */
339
+ }, {
340
+ key: "reloadConnection",
341
+ value:
342
+ // equivalent to triggering didClose and didOpen, as per syncing specification,
343
+ // but also reloads the connection; used during file rename (or when it was moved)
344
+ function reloadConnection() {
345
+ // ignore premature calls (before the editor was initialized)
346
+ if (this.virtualDocument === null) {
347
+ return;
348
+ }
349
+
350
+ // disconnect all existing connections (and dispose adapters)
351
+ this.disconnect();
352
+
353
+ // recreate virtual document using current path and language
354
+ // as virtual editor assumes it gets the virtual document at init,
355
+ // just dispose virtual editor (which disposes virtual document too)
356
+ // and re-initialize both virtual editor and document
357
+ this.initVirtual();
358
+
359
+ // reconnect
360
+ this.connectDocument(this.virtualDocument, true).catch(console.warn);
361
+ }
362
+ }, {
363
+ key: "onConnected",
364
+ value:
365
+ /**
366
+ * Connect the virtual document with the language server.
367
+ */
368
+ function () {
369
+ var _onConnected = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(data) {
370
+ var virtualDocument;
371
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
372
+ while (1) switch (_context2.prev = _context2.next) {
373
+ case 0:
374
+ virtualDocument = data.virtualDocument;
375
+ this._adapterConnected.fire(data);
376
+ this._isConnected = true;
377
+ _context2.prev = 3;
378
+ _context2.next = 6;
379
+ return this.updateDocuments();
380
+ case 6:
381
+ _context2.next = 12;
382
+ break;
383
+ case 8:
384
+ _context2.prev = 8;
385
+ _context2.t0 = _context2["catch"](3);
386
+ console.warn('Could not update documents', _context2.t0);
387
+ return _context2.abrupt("return");
388
+ case 12:
389
+ // refresh the document on the LSP server
390
+ this.documentChanged(virtualDocument);
391
+ data.connection.serverNotifications['$/logTrace'].event(function (message) {
392
+ console.warn(data.connection.serverIdentifier, 'trace', virtualDocument.uri, message);
393
+ });
394
+ data.connection.serverNotifications['window/logMessage'].event(function (message) {
395
+ console.warn(data.connection.serverIdentifier + ': ' + message.message);
396
+ });
397
+ data.connection.serverNotifications['window/showMessage'].event(function (message) {
398
+ // void showDialog({
399
+ // title: this.trans.__('Message from ') + connection.serverIdentifier,
400
+ // body: message.message,
401
+ // });
402
+ alert("Message from ".concat(data.connection.serverIdentifier, ": ").concat(message.message));
403
+ });
404
+ data.connection.serverRequests['window/showMessageRequest'].setHandler( /*#__PURE__*/function () {
405
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(params) {
406
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
407
+ while (1) switch (_context.prev = _context.next) {
408
+ case 0:
409
+ alert("Message from ".concat(data.connection.serverIdentifier, ": ").concat(params.message));
410
+ return _context.abrupt("return", null);
411
+ case 2:
412
+ case "end":
413
+ return _context.stop();
414
+ }
415
+ }, _callee);
416
+ }));
417
+ return function (_x2) {
418
+ return _ref2.apply(this, arguments);
419
+ };
420
+ }());
421
+ case 17:
422
+ case "end":
423
+ return _context2.stop();
424
+ }
425
+ }, _callee2, this, [[3, 8]]);
426
+ }));
427
+ function onConnected(_x) {
428
+ return _onConnected.apply(this, arguments);
429
+ }
430
+ return onConnected;
431
+ }()
432
+ /**
433
+ * Opens a connection for the document. The connection may or may
434
+ * not be initialized, yet, and depending on when this is called, the client
435
+ * may not be fully connected.
436
+ *
437
+ * @param virtualDocument a VirtualDocument
438
+ * @param sendOpen whether to open the document immediately
439
+ */
440
+ }, {
441
+ key: "connectDocument",
442
+ value: function () {
443
+ var _connectDocument = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(virtualDocument) {
444
+ var sendOpen,
445
+ connectionContext,
446
+ _args3 = arguments;
447
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
448
+ while (1) switch (_context3.prev = _context3.next) {
449
+ case 0:
450
+ sendOpen = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : false;
451
+ virtualDocument.foreignDocumentOpened(this.onForeignDocumentOpened, this);
452
+ _context3.next = 4;
453
+ return this._connect(virtualDocument).catch(console.error);
454
+ case 4:
455
+ connectionContext = _context3.sent;
456
+ if (connectionContext && connectionContext.connection) {
457
+ virtualDocument.changed(this.documentChanged, this);
458
+ if (sendOpen) {
459
+ connectionContext.connection.sendOpenWhenReady(virtualDocument.documentInfo);
460
+ }
461
+ }
462
+ case 6:
463
+ case "end":
464
+ return _context3.stop();
465
+ }
466
+ }, _callee3, this);
467
+ }));
468
+ function connectDocument(_x3) {
469
+ return _connectDocument.apply(this, arguments);
470
+ }
471
+ return connectDocument;
472
+ }()
473
+ /**
474
+ * Create the virtual document using current path and language.
475
+ */
476
+ }, {
477
+ key: "initVirtual",
478
+ value: function initVirtual() {
479
+ var _this$_virtualDocumen,
480
+ _model$onSourceChange,
481
+ _this2 = this;
482
+ var model = this.widget.model;
483
+ (_this$_virtualDocumen = this._virtualDocument) === null || _this$_virtualDocumen === void 0 || _this$_virtualDocumen.dispose();
484
+ this._virtualDocument = this.createVirtualDocument();
485
+ (_model$onSourceChange = model.onSourceChanged) === null || _model$onSourceChange === void 0 || _model$onSourceChange.call(model, function () {
486
+ return _this2._onContentChanged();
487
+ });
488
+ }
489
+
490
+ /**
491
+ * Handler for opening a document contained in a parent document. The assumption
492
+ * is that the editor already exists for this, and as such the document
493
+ * should be queued for immediate opening.
494
+ *
495
+ * @param host the VirtualDocument that contains the VirtualDocument in another language
496
+ * @param context information about the foreign VirtualDocument
497
+ */
498
+ }, {
499
+ key: "onForeignDocumentOpened",
500
+ value: function () {
501
+ var _onForeignDocumentOpened = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(context) {
502
+ var foreignDocument;
503
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
504
+ while (1) switch (_context4.prev = _context4.next) {
505
+ case 0:
506
+ foreignDocument = context.foreignDocument;
507
+ _context4.next = 3;
508
+ return this.connectDocument(foreignDocument, true);
509
+ case 3:
510
+ foreignDocument.foreignDocumentClosed(this._onForeignDocumentClosed, this);
511
+ case 4:
512
+ case "end":
513
+ return _context4.stop();
514
+ }
515
+ }, _callee4, this);
516
+ }));
517
+ function onForeignDocumentOpened(_x4) {
518
+ return _onForeignDocumentOpened.apply(this, arguments);
519
+ }
520
+ return onForeignDocumentOpened;
521
+ }()
522
+ }, {
523
+ key: "_onForeignDocumentClosed",
524
+ value:
525
+ /**
526
+ * Callback called when a foreign document is closed,
527
+ * the associated signals with this virtual document
528
+ * are disconnected.
529
+ */
530
+ function _onForeignDocumentClosed(context) {
531
+ // const { foreignDocument } = context;
532
+ }
533
+
534
+ /**
535
+ * Detect the capabilities for the document type then
536
+ * open the websocket connection with the language server.
537
+ */
538
+ }, {
539
+ key: "_connect",
540
+ value: function () {
541
+ var _connect2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(virtualDocument) {
542
+ var language, capabilities, options, connection;
543
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
544
+ while (1) switch (_context5.prev = _context5.next) {
545
+ case 0:
546
+ language = virtualDocument.language;
547
+ capabilities = {
548
+ textDocument: {
549
+ synchronization: {
550
+ dynamicRegistration: true,
551
+ willSave: false,
552
+ didSave: true,
553
+ willSaveWaitUntil: false
554
+ }
555
+ },
556
+ workspace: {
557
+ didChangeConfiguration: {
558
+ dynamicRegistration: true
559
+ }
560
+ }
561
+ };
562
+ capabilities = mergeWith(capabilities, this.options.featureManager.clientCapabilities());
563
+ options = {
564
+ capabilities: capabilities,
565
+ virtualDocument: virtualDocument,
566
+ language: language,
567
+ hasLspSupportedFile: virtualDocument.hasLspSupportedFile
568
+ };
569
+ _context5.next = 6;
570
+ return this.connectionManager.connect(options);
571
+ case 6:
572
+ connection = _context5.sent;
573
+ if (!connection) {
574
+ _context5.next = 13;
575
+ break;
576
+ }
577
+ _context5.next = 10;
578
+ return this.onConnected({
579
+ virtualDocument: virtualDocument,
580
+ connection: connection
581
+ });
582
+ case 10:
583
+ return _context5.abrupt("return", {
584
+ connection: connection,
585
+ virtualDocument: virtualDocument
586
+ });
587
+ case 13:
588
+ return _context5.abrupt("return", undefined);
589
+ case 14:
590
+ case "end":
591
+ return _context5.stop();
592
+ }
593
+ }, _callee5, this);
594
+ }));
595
+ function _connect(_x5) {
596
+ return _connect2.apply(this, arguments);
597
+ }
598
+ return _connect;
599
+ }()
600
+ /**
601
+ * Handle content changes and update all virtual documents after a change.
602
+ *
603
+ * #### Notes
604
+ * Update to the state of a notebook may be done without a notice on the
605
+ * CodeMirror level, e.g. when a cell is deleted. Therefore a
606
+ * JupyterLab-specific signal is watched instead.
607
+ *
608
+ * While by not using the change event of CodeMirror editors we lose an easy
609
+ * way to send selective (range) updates this can be still implemented by
610
+ * comparison of before/after states of the virtual documents, which is
611
+ * more resilient and editor-independent.
612
+ */
613
+ }, {
614
+ key: "_onContentChanged",
615
+ value: function () {
616
+ var _onContentChanged2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
617
+ var promise;
618
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
619
+ while (1) switch (_context6.prev = _context6.next) {
620
+ case 0:
621
+ // Update the virtual documents.
622
+ // Sending the updates to LSP is out of scope here.
623
+ promise = this.updateDocuments();
624
+ if (promise) {
625
+ _context6.next = 4;
626
+ break;
627
+ }
628
+ console.warn('Could not update documents');
629
+ return _context6.abrupt("return");
630
+ case 4:
631
+ this._updateFinished = promise.catch(console.warn);
632
+ _context6.next = 7;
633
+ return this.updateFinished;
634
+ case 7:
635
+ case "end":
636
+ return _context6.stop();
637
+ }
638
+ }, _callee6, this);
639
+ }));
640
+ function _onContentChanged() {
641
+ return _onContentChanged2.apply(this, arguments);
642
+ }
643
+ return _onContentChanged;
644
+ }()
645
+ }]);
646
+ return WidgetLSPAdapter;
647
+ }();