"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

require("core-js/modules/es.array.slice.js");

require("core-js/modules/es.function.name.js");

require("core-js/modules/es.array.from.js");

require("core-js/modules/es.regexp.exec.js");

require("core-js/modules/es.symbol.js");

require("core-js/modules/es.symbol.description.js");

require("core-js/modules/es.symbol.iterator.js");

require("regenerator-runtime/runtime.js");

require("core-js/modules/es.array.iterator.js");

require("core-js/modules/es.map.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/es.string.iterator.js");

require("core-js/modules/web.dom-collections.iterator.js");

require("core-js/modules/es.regexp.to-string.js");

require("core-js/modules/es.array.map.js");

require("core-js/modules/es.array.filter.js");

require("core-js/modules/es.promise.js");

require("core-js/modules/es.typed-array.uint8-array.js");

require("core-js/modules/es.typed-array.copy-within.js");

require("core-js/modules/es.typed-array.every.js");

require("core-js/modules/es.typed-array.fill.js");

require("core-js/modules/es.typed-array.filter.js");

require("core-js/modules/es.typed-array.find.js");

require("core-js/modules/es.typed-array.find-index.js");

require("core-js/modules/es.typed-array.for-each.js");

require("core-js/modules/es.typed-array.includes.js");

require("core-js/modules/es.typed-array.index-of.js");

require("core-js/modules/es.typed-array.iterator.js");

require("core-js/modules/es.typed-array.join.js");

require("core-js/modules/es.typed-array.last-index-of.js");

require("core-js/modules/es.typed-array.map.js");

require("core-js/modules/es.typed-array.reduce.js");

require("core-js/modules/es.typed-array.reduce-right.js");

require("core-js/modules/es.typed-array.reverse.js");

require("core-js/modules/es.typed-array.set.js");

require("core-js/modules/es.typed-array.slice.js");

require("core-js/modules/es.typed-array.some.js");

require("core-js/modules/es.typed-array.sort.js");

require("core-js/modules/es.typed-array.subarray.js");

require("core-js/modules/es.typed-array.to-locale-string.js");

require("core-js/modules/es.typed-array.to-string.js");

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; } } }; }

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); }

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; }

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); } }

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); }); }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

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, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.VsCodeClientWorkspace = void 0;

var vscode_languageserver_1 = require("vscode-languageserver");

var vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");

var md = require("vscode-markdown-languageservice");

var vscode_uri_1 = require("vscode-uri");

var protocol = require("./protocol");

var file_1 = require("./util/file");

var limiter_1 = require("./util/limiter");

var resourceMap_1 = require("./util/resourceMap");

var schemes_1 = require("./util/schemes");

var VsCodeDocument = /*#__PURE__*/function () {
  function VsCodeDocument(uri, init) {
    _classCallCheck(this, VsCodeDocument);

    this.uri = uri;
    this.inMemoryDoc = init === null || init === void 0 ? void 0 : init.inMemoryDoc;
    this.onDiskDoc = init === null || init === void 0 ? void 0 : init.onDiskDoc;
  }

  _createClass(VsCodeDocument, [{
    key: "version",
    get: function get() {
      var _ref, _this$inMemoryDoc$ver, _this$inMemoryDoc, _this$onDiskDoc;

      return (_ref = (_this$inMemoryDoc$ver = (_this$inMemoryDoc = this.inMemoryDoc) === null || _this$inMemoryDoc === void 0 ? void 0 : _this$inMemoryDoc.version) !== null && _this$inMemoryDoc$ver !== void 0 ? _this$inMemoryDoc$ver : (_this$onDiskDoc = this.onDiskDoc) === null || _this$onDiskDoc === void 0 ? void 0 : _this$onDiskDoc.version) !== null && _ref !== void 0 ? _ref : 0;
    }
  }, {
    key: "lineCount",
    get: function get() {
      var _ref2, _this$inMemoryDoc$lin, _this$inMemoryDoc2, _this$onDiskDoc2;

      return (_ref2 = (_this$inMemoryDoc$lin = (_this$inMemoryDoc2 = this.inMemoryDoc) === null || _this$inMemoryDoc2 === void 0 ? void 0 : _this$inMemoryDoc2.lineCount) !== null && _this$inMemoryDoc$lin !== void 0 ? _this$inMemoryDoc$lin : (_this$onDiskDoc2 = this.onDiskDoc) === null || _this$onDiskDoc2 === void 0 ? void 0 : _this$onDiskDoc2.lineCount) !== null && _ref2 !== void 0 ? _ref2 : 0;
    }
  }, {
    key: "getText",
    value: function getText(range) {
      if (this.inMemoryDoc) {
        return this.inMemoryDoc.getText(range);
      }

      if (this.onDiskDoc) {
        return this.onDiskDoc.getText(range);
      }

      throw new Error('Document has been closed');
    }
  }, {
    key: "positionAt",
    value: function positionAt(offset) {
      if (this.inMemoryDoc) {
        return this.inMemoryDoc.positionAt(offset);
      }

      if (this.onDiskDoc) {
        return this.onDiskDoc.positionAt(offset);
      }

      throw new Error('Document has been closed');
    }
  }, {
    key: "offsetAt",
    value: function offsetAt(position) {
      if (this.inMemoryDoc) {
        return this.inMemoryDoc.offsetAt(position);
      }

      if (this.onDiskDoc) {
        return this.onDiskDoc.offsetAt(position);
      }

      throw new Error('Document has been closed');
    }
  }, {
    key: "hasInMemoryDoc",
    value: function hasInMemoryDoc() {
      return !!this.inMemoryDoc;
    }
  }, {
    key: "isDetached",
    value: function isDetached() {
      return !this.onDiskDoc && !this.inMemoryDoc;
    }
  }, {
    key: "setInMemoryDoc",
    value: function setInMemoryDoc(doc) {
      this.inMemoryDoc = doc;
    }
  }, {
    key: "setOnDiskDoc",
    value: function setOnDiskDoc(doc) {
      this.onDiskDoc = doc;
    }
  }]);

  return VsCodeDocument;
}();

var VsCodeClientWorkspace = /*#__PURE__*/function () {
  function VsCodeClientWorkspace(connection, config, documents, notebooks, logger) {
    var _this = this;

    _classCallCheck(this, VsCodeClientWorkspace);

    this.connection = connection;
    this.config = config;
    this.documents = documents;
    this.notebooks = notebooks;
    this.logger = logger;
    this._onDidCreateMarkdownDocument = new vscode_languageserver_1.Emitter();
    this.onDidCreateMarkdownDocument = this._onDidCreateMarkdownDocument.event;
    this._onDidChangeMarkdownDocument = new vscode_languageserver_1.Emitter();
    this.onDidChangeMarkdownDocument = this._onDidChangeMarkdownDocument.event;
    this._onDidDeleteMarkdownDocument = new vscode_languageserver_1.Emitter();
    this.onDidDeleteMarkdownDocument = this._onDidDeleteMarkdownDocument.event;
    this._documentCache = new resourceMap_1.ResourceMap();
    this._utf8Decoder = new TextDecoder('utf-8');
    this._watcherPool = 0;
    this._watchers = new Map();
    this._workspaceFolders = [];
    documents.onDidOpen(function (e) {
      if (!_this.isRelevantMarkdownDocument(e.document)) {
        return;
      }

      _this.logger.log(md.LogLevel.Trace, 'VsCodeClientWorkspace.TextDocument.onDidOpen', {
        document: e.document.uri
      });

      var uri = vscode_uri_1.URI.parse(e.document.uri);

      var doc = _this._documentCache.get(uri);

      if (doc) {
        // File already existed on disk
        doc.setInMemoryDoc(e.document); // The content visible to the language service may have changed since the in-memory doc
        // may differ from the one on-disk. To be safe we always fire a change event.

        _this._onDidChangeMarkdownDocument.fire(doc);
      } else {
        // We're creating the file for the first time
        var _doc = new VsCodeDocument(e.document.uri, {
          inMemoryDoc: e.document
        });

        _this._documentCache.set(uri, _doc);

        _this._onDidCreateMarkdownDocument.fire(_doc);
      }
    });
    documents.onDidChangeContent(function (e) {
      if (!_this.isRelevantMarkdownDocument(e.document)) {
        return;
      }

      _this.logger.log(md.LogLevel.Trace, 'VsCodeClientWorkspace.TextDocument.onDidChanceContent', {
        document: e.document.uri
      });

      var uri = vscode_uri_1.URI.parse(e.document.uri);

      var entry = _this._documentCache.get(uri);

      if (entry) {
        entry.setInMemoryDoc(e.document);

        _this._onDidChangeMarkdownDocument.fire(entry);
      }
    });
    documents.onDidClose( /*#__PURE__*/function () {
      var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(e) {
        var uri, doc;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (_this.isRelevantMarkdownDocument(e.document)) {
                  _context.next = 2;
                  break;
                }

                return _context.abrupt("return");

              case 2:
                _this.logger.log(md.LogLevel.Trace, 'VsCodeClientWorkspace.TextDocument.onDidClose', {
                  document: e.document.uri
                });

                uri = vscode_uri_1.URI.parse(e.document.uri);
                doc = _this._documentCache.get(uri);

                if (doc) {
                  _context.next = 7;
                  break;
                }

                return _context.abrupt("return");

              case 7:
                doc.setInMemoryDoc(undefined);

                if (!doc.isDetached()) {
                  _context.next = 11;
                  break;
                }

                // The document has been fully closed
                _this.doDeleteDocument(uri);

                return _context.abrupt("return");

              case 11:
                _context.next = 13;
                return _this.statBypassingCache(uri);

              case 13:
                if (_context.sent) {
                  _context.next = 17;
                  break;
                }

                if (!(_this._documentCache.get(uri) === doc && !doc.hasInMemoryDoc())) {
                  _context.next = 17;
                  break;
                }

                _this.doDeleteDocument(uri);

                return _context.abrupt("return");

              case 17:
                // The document still exists on disk
                // To be safe, tell the service that the document has changed because the
                // in-memory doc contents may be different than the disk doc contents.
                _this._onDidChangeMarkdownDocument.fire(doc);

              case 18:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      return function (_x) {
        return _ref3.apply(this, arguments);
      };
    }());
    connection.onDidChangeWatchedFiles( /*#__PURE__*/function () {
      var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(_ref4) {
        var changes, _iterator, _step, change, resource, entry, document, _entry, _document, _entry2;

        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                changes = _ref4.changes;
                _iterator = _createForOfIteratorHelper(changes);
                _context2.prev = 2;

                _iterator.s();

              case 4:
                if ((_step = _iterator.n()).done) {
                  _context2.next = 30;
                  break;
                }

                change = _step.value;
                resource = vscode_uri_1.URI.parse(change.uri);

                _this.logger.log(md.LogLevel.Trace, 'VsCodeClientWorkspace.onDidChangeWatchedFiles', {
                  type: change.type,
                  resource: resource.toString()
                });

                _context2.t0 = change.type;
                _context2.next = _context2.t0 === vscode_languageserver_1.FileChangeType.Changed ? 11 : _context2.t0 === vscode_languageserver_1.FileChangeType.Created ? 18 : _context2.t0 === vscode_languageserver_1.FileChangeType.Deleted ? 25 : 28;
                break;

              case 11:
                entry = _this._documentCache.get(resource);

                if (!entry) {
                  _context2.next = 17;
                  break;
                }

                _context2.next = 15;
                return _this.openMarkdownDocumentFromFs(resource);

              case 15:
                document = _context2.sent;

                if (document) {
                  _this._onDidChangeMarkdownDocument.fire(document);
                }

              case 17:
                return _context2.abrupt("break", 28);

              case 18:
                _entry = _this._documentCache.get(resource);

                if (!_entry) {
                  _context2.next = 24;
                  break;
                }

                _context2.next = 22;
                return _this.openMarkdownDocumentFromFs(resource);

              case 22:
                _document = _context2.sent;

                if (_document) {
                  _this._onDidCreateMarkdownDocument.fire(_document);
                }

              case 24:
                return _context2.abrupt("break", 28);

              case 25:
                _entry2 = _this._documentCache.get(resource);

                if (_entry2) {
                  _entry2.setOnDiskDoc(undefined);

                  if (_entry2.isDetached()) {
                    _this.doDeleteDocument(resource);
                  }
                }

                return _context2.abrupt("break", 28);

              case 28:
                _context2.next = 4;
                break;

              case 30:
                _context2.next = 35;
                break;

              case 32:
                _context2.prev = 32;
                _context2.t1 = _context2["catch"](2);

                _iterator.e(_context2.t1);

              case 35:
                _context2.prev = 35;

                _iterator.f();

                return _context2.finish(35);

              case 38:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, null, [[2, 32, 35, 38]]);
      }));

      return function (_x2) {
        return _ref5.apply(this, arguments);
      };
    }());
    connection.onRequest(protocol.fs_watcher_onChange, function (params) {
      _this.logger.log(md.LogLevel.Trace, 'VsCodeClientWorkspace.fs_watcher_onChange', {
        kind: params.kind,
        uri: params.uri
      });

      var watcher = _this._watchers.get(params.id);

      if (!watcher) {
        return;
      }

      switch (params.kind) {
        case 'create':
          watcher.onDidCreate.fire(vscode_uri_1.URI.parse(params.uri));
          return;

        case 'change':
          watcher.onDidChange.fire(vscode_uri_1.URI.parse(params.uri));
          return;

        case 'delete':
          watcher.onDidDelete.fire(vscode_uri_1.URI.parse(params.uri));
          return;
      }
    });
  }

  _createClass(VsCodeClientWorkspace, [{
    key: "listen",
    value: function listen() {
      var _this2 = this;

      this.connection.workspace.onDidChangeWorkspaceFolders( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
        var _yield$_this2$connect;

        return regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _context3.next = 2;
                return _this2.connection.workspace.getWorkspaceFolders();

              case 2:
                _context3.t1 = _yield$_this2$connect = _context3.sent;
                _context3.t0 = _context3.t1 !== null;

                if (!_context3.t0) {
                  _context3.next = 6;
                  break;
                }

                _context3.t0 = _yield$_this2$connect !== void 0;

              case 6:
                if (!_context3.t0) {
                  _context3.next = 10;
                  break;
                }

                _context3.t2 = _yield$_this2$connect;
                _context3.next = 11;
                break;

              case 10:
                _context3.t2 = [];

              case 11:
                _this2.workspaceFolders = _context3.t2.map(function (x) {
                  return vscode_uri_1.URI.parse(x.uri);
                });

              case 12:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3);
      })));
    }
  }, {
    key: "workspaceFolders",
    get: function get() {
      return this._workspaceFolders;
    },
    set: function set(value) {
      this._workspaceFolders = value;
    }
  }, {
    key: "getAllMarkdownDocuments",
    value: function () {
      var _getAllMarkdownDocuments = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() {
        var _this3 = this;

        var openTextDocumentResults, allDocs, _iterator2, _step2, doc, maxConcurrent, limiter, resources;

        return regeneratorRuntime.wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                // Add opened files (such as untitled files)
                openTextDocumentResults = this.documents.all().filter(function (doc) {
                  return _this3.isRelevantMarkdownDocument(doc);
                });
                allDocs = new resourceMap_1.ResourceMap();
                _iterator2 = _createForOfIteratorHelper(openTextDocumentResults);

                try {
                  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
                    doc = _step2.value;
                    allDocs.set(vscode_uri_1.URI.parse(doc.uri), doc);
                  } // And then add files on disk

                } catch (err) {
                  _iterator2.e(err);
                } finally {
                  _iterator2.f();
                }

                maxConcurrent = 20;
                limiter = new limiter_1.Limiter(maxConcurrent);
                _context5.next = 8;
                return this.connection.sendRequest(protocol.findMarkdownFilesInWorkspace, {});

              case 8:
                resources = _context5.sent;
                _context5.next = 11;
                return Promise.all(resources.map(function (strResource) {
                  return limiter.queue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
                    var resource, doc;
                    return regeneratorRuntime.wrap(function _callee4$(_context4) {
                      while (1) {
                        switch (_context4.prev = _context4.next) {
                          case 0:
                            resource = vscode_uri_1.URI.parse(strResource);

                            if (!allDocs.has(resource)) {
                              _context4.next = 3;
                              break;
                            }

                            return _context4.abrupt("return");

                          case 3:
                            _context4.next = 5;
                            return _this3.openMarkdownDocument(resource);

                          case 5:
                            doc = _context4.sent;

                            if (doc) {
                              allDocs.set(resource, doc);
                            }

                            return _context4.abrupt("return", doc);

                          case 8:
                          case "end":
                            return _context4.stop();
                        }
                      }
                    }, _callee4);
                  })));
                }));

              case 11:
                return _context5.abrupt("return", allDocs.values());

              case 12:
              case "end":
                return _context5.stop();
            }
          }
        }, _callee5, this);
      }));

      function getAllMarkdownDocuments() {
        return _getAllMarkdownDocuments.apply(this, arguments);
      }

      return getAllMarkdownDocuments;
    }()
  }, {
    key: "hasMarkdownDocument",
    value: function hasMarkdownDocument(resource) {
      return !!this.documents.get(resource.toString());
    }
  }, {
    key: "openMarkdownDocument",
    value: function () {
      var _openMarkdownDocument = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(resource) {
        var existing, matchingDocument, entry;
        return regeneratorRuntime.wrap(function _callee6$(_context6) {
          while (1) {
            switch (_context6.prev = _context6.next) {
              case 0:
                existing = this._documentCache.get(resource);

                if (!existing) {
                  _context6.next = 3;
                  break;
                }

                return _context6.abrupt("return", existing);

              case 3:
                matchingDocument = this.documents.get(resource.toString());

                if (!matchingDocument) {
                  _context6.next = 8;
                  break;
                }

                entry = this._documentCache.get(resource);

                if (entry) {
                  entry.setInMemoryDoc(matchingDocument);
                } else {
                  entry = new VsCodeDocument(resource.toString(), {
                    inMemoryDoc: matchingDocument
                  });

                  this._documentCache.set(resource, entry);
                }

                return _context6.abrupt("return", entry);

              case 8:
                return _context6.abrupt("return", this.openMarkdownDocumentFromFs(resource));

              case 9:
              case "end":
                return _context6.stop();
            }
          }
        }, _callee6, this);
      }));

      function openMarkdownDocument(_x3) {
        return _openMarkdownDocument.apply(this, arguments);
      }

      return openMarkdownDocument;
    }()
  }, {
    key: "openMarkdownDocumentFromFs",
    value: function () {
      var _openMarkdownDocumentFromFs = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(resource) {
        var response, bytes, text, doc;
        return regeneratorRuntime.wrap(function _callee7$(_context7) {
          while (1) {
            switch (_context7.prev = _context7.next) {
              case 0:
                if ((0, file_1.looksLikeMarkdownPath)(this.config, resource)) {
                  _context7.next = 2;
                  break;
                }

                return _context7.abrupt("return", undefined);

              case 2:
                _context7.prev = 2;
                _context7.next = 5;
                return this.connection.sendRequest(protocol.fs_readFile, {
                  uri: resource.toString()
                });

              case 5:
                response = _context7.sent;
                // TODO: LSP doesn't seem to handle Array buffers well
                bytes = new Uint8Array(response); // We assume that markdown is in UTF-8

                text = this._utf8Decoder.decode(bytes);
                doc = new VsCodeDocument(resource.toString(), {
                  onDiskDoc: vscode_languageserver_textdocument_1.TextDocument.create(resource.toString(), 'markdown', 0, text)
                });

                this._documentCache.set(resource, doc);

                return _context7.abrupt("return", doc);

              case 13:
                _context7.prev = 13;
                _context7.t0 = _context7["catch"](2);
                return _context7.abrupt("return", undefined);

              case 16:
              case "end":
                return _context7.stop();
            }
          }
        }, _callee7, this, [[2, 13]]);
      }));

      function openMarkdownDocumentFromFs(_x4) {
        return _openMarkdownDocumentFromFs.apply(this, arguments);
      }

      return openMarkdownDocumentFromFs;
    }()
  }, {
    key: "stat",
    value: function () {
      var _stat = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8(resource) {
        return regeneratorRuntime.wrap(function _callee8$(_context8) {
          while (1) {
            switch (_context8.prev = _context8.next) {
              case 0:
                this.logger.log(md.LogLevel.Trace, 'VsCodeClientWorkspace.stat', {
                  resource: resource.toString()
                });

                if (!this._documentCache.has(resource)) {
                  _context8.next = 3;
                  break;
                }

                return _context8.abrupt("return", {
                  isDirectory: false
                });

              case 3:
                return _context8.abrupt("return", this.statBypassingCache(resource));

              case 4:
              case "end":
                return _context8.stop();
            }
          }
        }, _callee8, this);
      }));

      function stat(_x5) {
        return _stat.apply(this, arguments);
      }

      return stat;
    }()
  }, {
    key: "statBypassingCache",
    value: function () {
      var _statBypassingCache = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9(resource) {
        var uri, fsResult;
        return regeneratorRuntime.wrap(function _callee9$(_context9) {
          while (1) {
            switch (_context9.prev = _context9.next) {
              case 0:
                uri = resource.toString();

                if (!this.documents.get(uri)) {
                  _context9.next = 3;
                  break;
                }

                return _context9.abrupt("return", {
                  isDirectory: false
                });

              case 3:
                _context9.next = 5;
                return this.connection.sendRequest(protocol.fs_stat, {
                  uri: uri
                });

              case 5:
                fsResult = _context9.sent;
                return _context9.abrupt("return", fsResult !== null && fsResult !== void 0 ? fsResult : undefined);

              case 7:
              case "end":
                return _context9.stop();
            }
          }
        }, _callee9, this);
      }));

      function statBypassingCache(_x6) {
        return _statBypassingCache.apply(this, arguments);
      }

      return statBypassingCache;
    }()
  }, {
    key: "readDirectory",
    value: function () {
      var _readDirectory = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10(resource) {
        return regeneratorRuntime.wrap(function _callee10$(_context10) {
          while (1) {
            switch (_context10.prev = _context10.next) {
              case 0:
                this.logger.log(md.LogLevel.Trace, 'VsCodeClientWorkspace.readDir', {
                  resource: resource.toString()
                });
                return _context10.abrupt("return", this.connection.sendRequest(protocol.fs_readDirectory, {
                  uri: resource.toString()
                }));

              case 2:
              case "end":
                return _context10.stop();
            }
          }
        }, _callee10, this);
      }));

      function readDirectory(_x7) {
        return _readDirectory.apply(this, arguments);
      }

      return readDirectory;
    }()
  }, {
    key: "getContainingDocument",
    value: function getContainingDocument(resource) {
      if (resource.scheme === schemes_1.Schemes.notebookCell) {
        var nb = this.notebooks.findNotebookDocumentForCell(resource.toString());

        if (nb) {
          return {
            uri: vscode_uri_1.URI.parse(nb.uri),
            children: nb.cells.map(function (cell) {
              return {
                uri: vscode_uri_1.URI.parse(cell.document)
              };
            })
          };
        }
      }

      return undefined;
    }
  }, {
    key: "watchFile",
    value: function watchFile(resource, options) {
      var _this4 = this;

      var id = this._watcherPool++;
      this.logger.log(md.LogLevel.Trace, 'VsCodeClientWorkspace.watchFile', {
        id: id,
        resource: resource.toString()
      });
      var entry = {
        resource: resource,
        options: options,
        onDidCreate: new vscode_languageserver_1.Emitter(),
        onDidChange: new vscode_languageserver_1.Emitter(),
        onDidDelete: new vscode_languageserver_1.Emitter()
      };

      this._watchers.set(id, entry);

      this.connection.sendRequest(protocol.fs_watcher_create, {
        id: id,
        uri: resource.toString(),
        options: options,
        watchParentDirs: true
      });
      return {
        onDidCreate: entry.onDidCreate.event,
        onDidChange: entry.onDidChange.event,
        onDidDelete: entry.onDidDelete.event,
        dispose: function dispose() {
          _this4.logger.log(md.LogLevel.Trace, 'VsCodeClientWorkspace.disposeWatcher', {
            id: id,
            resource: resource.toString()
          });

          _this4.connection.sendRequest(protocol.fs_watcher_delete, {
            id: id
          });

          _this4._watchers.delete(id);
        }
      };
    }
  }, {
    key: "isRelevantMarkdownDocument",
    value: function isRelevantMarkdownDocument(doc) {
      return (0, file_1.isMarkdownFile)(doc) && vscode_uri_1.URI.parse(doc.uri).scheme !== 'vscode-bulkeditpreview';
    }
  }, {
    key: "doDeleteDocument",
    value: function doDeleteDocument(uri) {
      this.logger.log(md.LogLevel.Trace, 'VsCodeClientWorkspace.deleteDocument', {
        document: uri.toString()
      });

      this._documentCache.delete(uri);

      this._onDidDeleteMarkdownDocument.fire(uri);
    }
  }]);

  return VsCodeClientWorkspace;
}();

exports.VsCodeClientWorkspace = VsCodeClientWorkspace;