"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.object.keys.js");

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

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

require("core-js/modules/es.object.get-own-property-descriptor.js");

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

require("core-js/modules/es.object.get-own-property-descriptors.js");

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

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

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

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

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

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

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

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

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.startVsCodeServer = startVsCodeServer;
exports.startServer = startServer;

var l10n = require("@vscode/l10n");

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

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

var lsp = require("vscode-languageserver-types");

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

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

var config_1 = require("./config");

var configuration_1 = require("./configuration");

var diagnostics_1 = require("./languageFeatures/diagnostics");

var logging_1 = require("./logging");

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

var workspace_1 = require("./workspace");

var organizeLinkDefKind = 'source.organizeLinkDefinitions';

function startVsCodeServer(_x) {
  return _startVsCodeServer.apply(this, arguments);
}

function _startVsCodeServer() {
  _startVsCodeServer = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(connection) {
    var configurationManager, logger, parser, documents, notebooks, workspaceFactory;
    return regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            configurationManager = new configuration_1.ConfigurationManager(connection);
            logger = new logging_1.LogFunctionLogger(connection.console.log.bind(connection.console), configurationManager);
            parser = new ( /*#__PURE__*/function () {
              function _class() {
                _classCallCheck(this, _class);

                this.slugifier = md.githubSlugifier;
              }

              _createClass(_class, [{
                key: "tokenize",
                value: function tokenize(document) {
                  return connection.sendRequest(protocol.parse, {
                    uri: document.uri,
                    // Clients won't be able to read temp documents.
                    // Send along the full text for parsing.
                    text: document.version < 0 ? document.getText() : undefined
                  });
                }
              }]);

              return _class;
            }())();
            documents = new vscode_languageserver_1.TextDocuments(vscode_languageserver_textdocument_1.TextDocument);
            notebooks = new vscode_languageserver_1.NotebookDocuments(documents);

            workspaceFactory = function workspaceFactory(_ref3) {
              var connection = _ref3.connection,
                  config = _ref3.config,
                  workspaceFolders = _ref3.workspaceFolders;
              var workspace = new workspace_1.VsCodeClientWorkspace(connection, config, documents, notebooks, logger);
              workspace.workspaceFolders = (workspaceFolders !== null && workspaceFolders !== void 0 ? workspaceFolders : []).map(function (x) {
                return vscode_uri_1.URI.parse(x.uri);
              });
              return workspace;
            };

            return _context3.abrupt("return", startServer(connection, {
              documents: documents,
              notebooks: notebooks,
              configurationManager: configurationManager,
              logger: logger,
              parser: parser,
              workspaceFactory: workspaceFactory
            }));

          case 7:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _startVsCodeServer.apply(this, arguments);
}

function startServer(_x2, _x3) {
  return _startServer.apply(this, arguments);
}

function _startServer() {
  _startServer = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee22(connection, serverConfig) {
    var documents, notebooks, mdLs;
    return regeneratorRuntime.wrap(function _callee22$(_context22) {
      while (1) {
        switch (_context22.prev = _context22.next) {
          case 0:
            documents = serverConfig.documents, notebooks = serverConfig.notebooks;
            connection.onInitialize(function (params) {
              var initOptions = params.initializationOptions;
              var mdConfig = (0, config_1.getLsConfiguration)(initOptions !== null && initOptions !== void 0 ? initOptions : {});
              var workspace = serverConfig.workspaceFactory({
                connection: connection,
                config: mdConfig,
                workspaceFolders: params.workspaceFolders
              });
              mdLs = md.createLanguageService(_objectSpread(_objectSpread({
                workspace: workspace,
                parser: serverConfig.parser,
                logger: serverConfig.logger
              }, mdConfig), {}, {
                get preferredMdPathExtensionStyle() {
                  var _serverConfig$configu;

                  switch ((_serverConfig$configu = serverConfig.configurationManager.getSettings()) === null || _serverConfig$configu === void 0 ? void 0 : _serverConfig$configu.markdown.preferredMdPathExtensionStyle) {
                    case 'includeExtension':
                      return md.PreferredMdPathExtensionStyle.includeExtension;

                    case 'removeExtension':
                      return md.PreferredMdPathExtensionStyle.removeExtension;

                    case 'auto':
                    default:
                      return md.PreferredMdPathExtensionStyle.auto;
                  }
                }

              }));
              registerCompletionsSupport(connection, documents, mdLs, serverConfig.configurationManager);
              registerDocumentHighlightSupport(connection, documents, mdLs, serverConfig.configurationManager);
              (0, diagnostics_1.registerValidateSupport)(connection, workspace, documents, mdLs, serverConfig.configurationManager, serverConfig.logger);
              return {
                capabilities: {
                  diagnosticProvider: {
                    documentSelector: null,
                    identifier: 'markdown',
                    interFileDependencies: true,
                    workspaceDiagnostics: false
                  },
                  codeActionProvider: {
                    resolveProvider: true,
                    codeActionKinds: [organizeLinkDefKind, 'quickfix', 'refactor']
                  },
                  definitionProvider: true,
                  documentLinkProvider: {
                    resolveProvider: true
                  },
                  documentSymbolProvider: true,
                  foldingRangeProvider: true,
                  hoverProvider: true,
                  referencesProvider: true,
                  renameProvider: {
                    prepareProvider: true
                  },
                  selectionRangeProvider: true,
                  workspaceSymbolProvider: true,
                  workspace: {
                    workspaceFolders: {
                      supported: true,
                      changeNotifications: true
                    }
                  }
                }
              };
            });
            connection.onDocumentLinks( /*#__PURE__*/function () {
              var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(params, token) {
                var document;
                return regeneratorRuntime.wrap(function _callee4$(_context4) {
                  while (1) {
                    switch (_context4.prev = _context4.next) {
                      case 0:
                        document = documents.get(params.textDocument.uri);

                        if (document) {
                          _context4.next = 3;
                          break;
                        }

                        return _context4.abrupt("return", []);

                      case 3:
                        return _context4.abrupt("return", mdLs.getDocumentLinks(document, token));

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

              return function (_x8, _x9) {
                return _ref4.apply(this, arguments);
              };
            }());
            connection.onDocumentLinkResolve( /*#__PURE__*/function () {
              var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(link, token) {
                return regeneratorRuntime.wrap(function _callee5$(_context5) {
                  while (1) {
                    switch (_context5.prev = _context5.next) {
                      case 0:
                        return _context5.abrupt("return", mdLs.resolveDocumentLink(link, token));

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

              return function (_x10, _x11) {
                return _ref5.apply(this, arguments);
              };
            }());
            connection.onDocumentSymbol( /*#__PURE__*/function () {
              var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(params, token) {
                var document;
                return regeneratorRuntime.wrap(function _callee6$(_context6) {
                  while (1) {
                    switch (_context6.prev = _context6.next) {
                      case 0:
                        document = documents.get(params.textDocument.uri);

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

                        return _context6.abrupt("return", []);

                      case 3:
                        return _context6.abrupt("return", mdLs.getDocumentSymbols(document, {
                          includeLinkDefinitions: true
                        }, token));

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

              return function (_x12, _x13) {
                return _ref6.apply(this, arguments);
              };
            }());
            connection.onFoldingRanges( /*#__PURE__*/function () {
              var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(params, token) {
                var document;
                return regeneratorRuntime.wrap(function _callee7$(_context7) {
                  while (1) {
                    switch (_context7.prev = _context7.next) {
                      case 0:
                        document = documents.get(params.textDocument.uri);

                        if (document) {
                          _context7.next = 3;
                          break;
                        }

                        return _context7.abrupt("return", []);

                      case 3:
                        return _context7.abrupt("return", mdLs.getFoldingRanges(document, token));

                      case 4:
                      case "end":
                        return _context7.stop();
                    }
                  }
                }, _callee7);
              }));

              return function (_x14, _x15) {
                return _ref7.apply(this, arguments);
              };
            }());
            connection.onSelectionRanges( /*#__PURE__*/function () {
              var _ref8 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8(params, token) {
                var document;
                return regeneratorRuntime.wrap(function _callee8$(_context8) {
                  while (1) {
                    switch (_context8.prev = _context8.next) {
                      case 0:
                        document = documents.get(params.textDocument.uri);

                        if (document) {
                          _context8.next = 3;
                          break;
                        }

                        return _context8.abrupt("return", []);

                      case 3:
                        return _context8.abrupt("return", mdLs.getSelectionRanges(document, params.positions, token));

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

              return function (_x16, _x17) {
                return _ref8.apply(this, arguments);
              };
            }());
            connection.onWorkspaceSymbol( /*#__PURE__*/function () {
              var _ref9 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9(params, token) {
                return regeneratorRuntime.wrap(function _callee9$(_context9) {
                  while (1) {
                    switch (_context9.prev = _context9.next) {
                      case 0:
                        return _context9.abrupt("return", mdLs.getWorkspaceSymbols(params.query, token));

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

              return function (_x18, _x19) {
                return _ref9.apply(this, arguments);
              };
            }());
            connection.onReferences( /*#__PURE__*/function () {
              var _ref10 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10(params, token) {
                var document;
                return regeneratorRuntime.wrap(function _callee10$(_context10) {
                  while (1) {
                    switch (_context10.prev = _context10.next) {
                      case 0:
                        document = documents.get(params.textDocument.uri);

                        if (document) {
                          _context10.next = 3;
                          break;
                        }

                        return _context10.abrupt("return", []);

                      case 3:
                        return _context10.abrupt("return", mdLs.getReferences(document, params.position, params.context, token));

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

              return function (_x20, _x21) {
                return _ref10.apply(this, arguments);
              };
            }());
            connection.onDefinition( /*#__PURE__*/function () {
              var _ref11 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee11(params, token) {
                var document;
                return regeneratorRuntime.wrap(function _callee11$(_context11) {
                  while (1) {
                    switch (_context11.prev = _context11.next) {
                      case 0:
                        document = documents.get(params.textDocument.uri);

                        if (document) {
                          _context11.next = 3;
                          break;
                        }

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

                      case 3:
                        return _context11.abrupt("return", mdLs.getDefinition(document, params.position, token));

                      case 4:
                      case "end":
                        return _context11.stop();
                    }
                  }
                }, _callee11);
              }));

              return function (_x22, _x23) {
                return _ref11.apply(this, arguments);
              };
            }());
            connection.onPrepareRename( /*#__PURE__*/function () {
              var _ref12 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee12(params, token) {
                var document;
                return regeneratorRuntime.wrap(function _callee12$(_context12) {
                  while (1) {
                    switch (_context12.prev = _context12.next) {
                      case 0:
                        document = documents.get(params.textDocument.uri);

                        if (document) {
                          _context12.next = 3;
                          break;
                        }

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

                      case 3:
                        _context12.prev = 3;
                        _context12.next = 6;
                        return mdLs.prepareRename(document, params.position, token);

                      case 6:
                        return _context12.abrupt("return", _context12.sent);

                      case 9:
                        _context12.prev = 9;
                        _context12.t0 = _context12["catch"](3);

                        if (!(_context12.t0 instanceof md.RenameNotSupportedAtLocationError)) {
                          _context12.next = 15;
                          break;
                        }

                        throw new vscode_languageserver_1.ResponseError(0, _context12.t0.message);

                      case 15:
                        throw _context12.t0;

                      case 16:
                      case "end":
                        return _context12.stop();
                    }
                  }
                }, _callee12, null, [[3, 9]]);
              }));

              return function (_x24, _x25) {
                return _ref12.apply(this, arguments);
              };
            }());
            connection.onRenameRequest( /*#__PURE__*/function () {
              var _ref13 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee13(params, token) {
                var document;
                return regeneratorRuntime.wrap(function _callee13$(_context13) {
                  while (1) {
                    switch (_context13.prev = _context13.next) {
                      case 0:
                        document = documents.get(params.textDocument.uri);

                        if (document) {
                          _context13.next = 3;
                          break;
                        }

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

                      case 3:
                        return _context13.abrupt("return", mdLs.getRenameEdit(document, params.position, params.newName, token));

                      case 4:
                      case "end":
                        return _context13.stop();
                    }
                  }
                }, _callee13);
              }));

              return function (_x26, _x27) {
                return _ref13.apply(this, arguments);
              };
            }());
            connection.onCodeAction( /*#__PURE__*/function () {
              var _ref14 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee14(params, token) {
                var _params$context$only;

                var document, action;
                return regeneratorRuntime.wrap(function _callee14$(_context14) {
                  while (1) {
                    switch (_context14.prev = _context14.next) {
                      case 0:
                        document = documents.get(params.textDocument.uri);

                        if (document) {
                          _context14.next = 3;
                          break;
                        }

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

                      case 3:
                        if (!((_params$context$only = params.context.only) !== null && _params$context$only !== void 0 && _params$context$only.some(function (kind) {
                          return kind === 'source' || kind.startsWith('source.');
                        }))) {
                          _context14.next = 6;
                          break;
                        }

                        action = {
                          title: l10n.t("Organize link definitions"),
                          kind: organizeLinkDefKind,
                          data: {
                            uri: document.uri
                          }
                        };
                        return _context14.abrupt("return", [action]);

                      case 6:
                        return _context14.abrupt("return", mdLs.getCodeActions(document, params.range, params.context, token));

                      case 7:
                      case "end":
                        return _context14.stop();
                    }
                  }
                }, _callee14);
              }));

              return function (_x28, _x29) {
                return _ref14.apply(this, arguments);
              };
            }());
            connection.onCodeActionResolve( /*#__PURE__*/function () {
              var _ref15 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee15(codeAction, token) {
                var _mdLs, data, document, edits;

                return regeneratorRuntime.wrap(function _callee15$(_context15) {
                  while (1) {
                    switch (_context15.prev = _context15.next) {
                      case 0:
                        if (!(codeAction.kind === organizeLinkDefKind)) {
                          _context15.next = 13;
                          break;
                        }

                        data = codeAction.data;
                        document = documents.get(data.uri);

                        if (document) {
                          _context15.next = 5;
                          break;
                        }

                        return _context15.abrupt("return", codeAction);

                      case 5:
                        _context15.next = 7;
                        return (_mdLs = mdLs) === null || _mdLs === void 0 ? void 0 : _mdLs.organizeLinkDefinitions(document, {
                          removeUnused: true
                        }, token);

                      case 7:
                        _context15.t0 = _context15.sent;

                        if (_context15.t0) {
                          _context15.next = 10;
                          break;
                        }

                        _context15.t0 = [];

                      case 10:
                        edits = _context15.t0;
                        codeAction.edit = {
                          changes: _defineProperty({}, data.uri, edits)
                        };
                        return _context15.abrupt("return", codeAction);

                      case 13:
                        return _context15.abrupt("return", codeAction);

                      case 14:
                      case "end":
                        return _context15.stop();
                    }
                  }
                }, _callee15);
              }));

              return function (_x30, _x31) {
                return _ref15.apply(this, arguments);
              };
            }());
            connection.onHover( /*#__PURE__*/function () {
              var _ref16 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee16(params, token) {
                var document;
                return regeneratorRuntime.wrap(function _callee16$(_context16) {
                  while (1) {
                    switch (_context16.prev = _context16.next) {
                      case 0:
                        document = documents.get(params.textDocument.uri);

                        if (document) {
                          _context16.next = 3;
                          break;
                        }

                        return _context16.abrupt("return", null);

                      case 3:
                        return _context16.abrupt("return", mdLs.getHover(document, params.position, token));

                      case 4:
                      case "end":
                        return _context16.stop();
                    }
                  }
                }, _callee16);
              }));

              return function (_x32, _x33) {
                return _ref16.apply(this, arguments);
              };
            }());
            connection.onRequest(protocol.getReferencesToFileInWorkspace, /*#__PURE__*/function () {
              var _ref17 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee17(params, token) {
                return regeneratorRuntime.wrap(function _callee17$(_context17) {
                  while (1) {
                    switch (_context17.prev = _context17.next) {
                      case 0:
                        return _context17.abrupt("return", mdLs.getFileReferences(vscode_uri_1.URI.parse(params.uri), token));

                      case 1:
                      case "end":
                        return _context17.stop();
                    }
                  }
                }, _callee17);
              }));

              return function (_x34, _x35) {
                return _ref17.apply(this, arguments);
              };
            }());
            connection.onRequest(protocol.getEditForFileRenames, /*#__PURE__*/function () {
              var _ref18 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee18(params, token) {
                var result;
                return regeneratorRuntime.wrap(function _callee18$(_context18) {
                  while (1) {
                    switch (_context18.prev = _context18.next) {
                      case 0:
                        _context18.next = 2;
                        return mdLs.getRenameFilesInWorkspaceEdit(params.map(function (x) {
                          return {
                            oldUri: vscode_uri_1.URI.parse(x.oldUri),
                            newUri: vscode_uri_1.URI.parse(x.newUri)
                          };
                        }), token);

                      case 2:
                        result = _context18.sent;

                        if (result) {
                          _context18.next = 5;
                          break;
                        }

                        return _context18.abrupt("return", result);

                      case 5:
                        return _context18.abrupt("return", {
                          edit: result.edit,
                          participatingRenames: result.participatingRenames.map(function (rename) {
                            return {
                              oldUri: rename.oldUri.toString(),
                              newUri: rename.newUri.toString()
                            };
                          })
                        });

                      case 6:
                      case "end":
                        return _context18.stop();
                    }
                  }
                }, _callee18);
              }));

              return function (_x36, _x37) {
                return _ref18.apply(this, arguments);
              };
            }());
            connection.onRequest(protocol.prepareUpdatePastedLinks, /*#__PURE__*/function () {
              var _ref19 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee19(params, token) {
                var document;
                return regeneratorRuntime.wrap(function _callee19$(_context19) {
                  while (1) {
                    switch (_context19.prev = _context19.next) {
                      case 0:
                        document = documents.get(params.uri);

                        if (document) {
                          _context19.next = 3;
                          break;
                        }

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

                      case 3:
                        return _context19.abrupt("return", mdLs.prepareUpdatePastedLinks(document, params.ranges, token));

                      case 4:
                      case "end":
                        return _context19.stop();
                    }
                  }
                }, _callee19);
              }));

              return function (_x38, _x39) {
                return _ref19.apply(this, arguments);
              };
            }());
            connection.onRequest(protocol.getUpdatePastedLinksEdit, /*#__PURE__*/function () {
              var _ref20 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee20(params, token) {
                var document, edits;
                return regeneratorRuntime.wrap(function _callee20$(_context20) {
                  while (1) {
                    switch (_context20.prev = _context20.next) {
                      case 0:
                        document = documents.get(params.pasteIntoDoc);

                        if (document) {
                          _context20.next = 3;
                          break;
                        }

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

                      case 3:
                        // TODO: Figure out why range types are lying
                        edits = params.edits.map(function (edit) {
                          return lsp.TextEdit.replace(lsp.Range.create(edit.range[0].line, edit.range[0].character, edit.range[1].line, edit.range[1].character), edit.newText);
                        });
                        return _context20.abrupt("return", mdLs.getUpdatePastedLinksEdit(document, edits, params.metadata, token));

                      case 5:
                      case "end":
                        return _context20.stop();
                    }
                  }
                }, _callee20);
              }));

              return function (_x40, _x41) {
                return _ref20.apply(this, arguments);
              };
            }());
            connection.onRequest(protocol.resolveLinkTarget, /*#__PURE__*/function () {
              var _ref21 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee21(params, token) {
                return regeneratorRuntime.wrap(function _callee21$(_context21) {
                  while (1) {
                    switch (_context21.prev = _context21.next) {
                      case 0:
                        return _context21.abrupt("return", mdLs.resolveLinkTarget(params.linkText, vscode_uri_1.URI.parse(params.uri), token));

                      case 1:
                      case "end":
                        return _context21.stop();
                    }
                  }
                }, _callee21);
              }));

              return function (_x42, _x43) {
                return _ref21.apply(this, arguments);
              };
            }());
            documents.listen(connection);
            notebooks === null || notebooks === void 0 ? void 0 : notebooks.listen(connection);
            connection.listen();

          case 23:
          case "end":
            return _context22.stop();
        }
      }
    }, _callee22);
  }));
  return _startServer.apply(this, arguments);
}

function registerDynamicClientFeature(config, isEnabled, register) {
  var registration;

  function update() {
    var settings = config.getSettings();

    if (isEnabled(settings)) {
      if (!registration) {
        registration = register();
      }
    } else {
      var _registration;

      (_registration = registration) === null || _registration === void 0 ? void 0 : _registration.then(function (x) {
        return x.dispose();
      });
      registration = undefined;
    }
  }

  update();
  return config.onDidChangeConfiguration(function () {
    return update();
  });
}

function registerCompletionsSupport(connection, documents, ls, config) {
  function getIncludeWorkspaceHeaderCompletions() {
    var _config$getSettings;

    switch ((_config$getSettings = config.getSettings()) === null || _config$getSettings === void 0 ? void 0 : _config$getSettings.markdown.suggest.paths.includeWorkspaceHeaderCompletions) {
      case 'onSingleOrDoubleHash':
        return md.IncludeWorkspaceHeaderCompletions.onSingleOrDoubleHash;

      case 'onDoubleHash':
        return md.IncludeWorkspaceHeaderCompletions.onDoubleHash;

      case 'never':
      default:
        return md.IncludeWorkspaceHeaderCompletions.never;
    }
  }

  connection.onCompletion( /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(params, token) {
      var settings, document;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              settings = config.getSettings();

              if (settings !== null && settings !== void 0 && settings.markdown.suggest.paths.enabled) {
                _context.next = 3;
                break;
              }

              return _context.abrupt("return", []);

            case 3:
              document = documents.get(params.textDocument.uri);

              if (!document) {
                _context.next = 6;
                break;
              }

              return _context.abrupt("return", ls.getCompletionItems(document, params.position, _objectSpread(_objectSpread({}, params.context || {}), {}, {
                includeWorkspaceHeaderCompletions: getIncludeWorkspaceHeaderCompletions()
              }), token));

            case 6:
              return _context.abrupt("return", []);

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

    return function (_x4, _x5) {
      return _ref.apply(this, arguments);
    };
  }());
  return registerDynamicClientFeature(config, function (settings) {
    return !!(settings !== null && settings !== void 0 && settings.markdown.suggest.paths.enabled);
  }, function () {
    var registrationOptions = {
      documentSelector: null,
      triggerCharacters: ['.', '/', '#']
    };
    return connection.client.register(vscode_languageserver_1.CompletionRequest.type, registrationOptions);
  });
}

function registerDocumentHighlightSupport(connection, documents, mdLs, configurationManager) {
  connection.onDocumentHighlight( /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(params, token) {
      var settings, document;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              settings = configurationManager.getSettings();

              if (settings !== null && settings !== void 0 && settings.markdown.occurrencesHighlight.enabled) {
                _context2.next = 3;
                break;
              }

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

            case 3:
              document = documents.get(params.textDocument.uri);

              if (document) {
                _context2.next = 6;
                break;
              }

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

            case 6:
              return _context2.abrupt("return", mdLs.getDocumentHighlights(document, params.position, token));

            case 7:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));

    return function (_x6, _x7) {
      return _ref2.apply(this, arguments);
    };
  }());
  return registerDynamicClientFeature(configurationManager, function (settings) {
    return !!(settings !== null && settings !== void 0 && settings.markdown.occurrencesHighlight.enabled);
  }, function () {
    var registrationOptions = {
      documentSelector: null
    };
    return connection.client.register(vscode_languageserver_1.DocumentHighlightRequest.type, registrationOptions);
  });
}