85 lines
3.7 KiB
JavaScript
85 lines
3.7 KiB
JavaScript
|
/* --------------------------------------------------------------------------------------------
|
||
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||
|
* ------------------------------------------------------------------------------------------ */
|
||
|
'use strict';
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const UUID = require("./utils/uuid");
|
||
|
const Is = require("./utils/is");
|
||
|
const vscode_1 = require("vscode");
|
||
|
const vscode_languageserver_protocol_1 = require("vscode-languageserver-protocol");
|
||
|
const client_1 = require("./client");
|
||
|
function ensure(target, key) {
|
||
|
if (target[key] === void 0) {
|
||
|
target[key] = {};
|
||
|
}
|
||
|
return target[key];
|
||
|
}
|
||
|
class FoldingRangeFeature extends client_1.TextDocumentFeature {
|
||
|
constructor(client) {
|
||
|
super(client, vscode_languageserver_protocol_1.FoldingRangeRequest.type);
|
||
|
}
|
||
|
fillClientCapabilities(capabilites) {
|
||
|
let capability = ensure(ensure(capabilites, 'textDocument'), 'foldingRange');
|
||
|
capability.dynamicRegistration = true;
|
||
|
capability.rangeLimit = 5000;
|
||
|
capability.lineFoldingOnly = true;
|
||
|
}
|
||
|
initialize(capabilities, documentSelector) {
|
||
|
if (!capabilities.foldingRangeProvider) {
|
||
|
return;
|
||
|
}
|
||
|
const implCapabilities = capabilities.foldingRangeProvider;
|
||
|
const id = Is.string(implCapabilities.id) && implCapabilities.id.length > 0 ? implCapabilities.id : UUID.generateUuid();
|
||
|
const selector = implCapabilities.documentSelector || documentSelector;
|
||
|
if (selector) {
|
||
|
this.register(this.messages, {
|
||
|
id,
|
||
|
registerOptions: Object.assign({}, { documentSelector: selector })
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
registerLanguageProvider(options) {
|
||
|
let client = this._client;
|
||
|
let provideFoldingRanges = (document, _, token) => {
|
||
|
const requestParams = {
|
||
|
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document)
|
||
|
};
|
||
|
return client.sendRequest(vscode_languageserver_protocol_1.FoldingRangeRequest.type, requestParams, token).then(this.asFoldingRanges.bind(this), (error) => {
|
||
|
client.logFailedRequest(vscode_languageserver_protocol_1.FoldingRangeRequest.type, error);
|
||
|
return Promise.resolve(null);
|
||
|
});
|
||
|
};
|
||
|
let middleware = client.clientOptions.middleware;
|
||
|
return vscode_1.languages.registerFoldingRangeProvider(options.documentSelector, {
|
||
|
provideFoldingRanges(document, context, token) {
|
||
|
return middleware.provideFoldingRanges
|
||
|
? middleware.provideFoldingRanges(document, context, token, provideFoldingRanges)
|
||
|
: provideFoldingRanges(document, context, token);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
asFoldingRangeKind(kind) {
|
||
|
if (kind) {
|
||
|
switch (kind) {
|
||
|
case vscode_languageserver_protocol_1.FoldingRangeKind.Comment:
|
||
|
return vscode_1.FoldingRangeKind.Comment;
|
||
|
case vscode_languageserver_protocol_1.FoldingRangeKind.Imports:
|
||
|
return vscode_1.FoldingRangeKind.Imports;
|
||
|
case vscode_languageserver_protocol_1.FoldingRangeKind.Region:
|
||
|
return vscode_1.FoldingRangeKind.Region;
|
||
|
}
|
||
|
}
|
||
|
return void 0;
|
||
|
}
|
||
|
asFoldingRanges(foldingRanges) {
|
||
|
if (Array.isArray(foldingRanges)) {
|
||
|
return foldingRanges.map(r => {
|
||
|
return new vscode_1.FoldingRange(r.startLine, r.endLine, this.asFoldingRangeKind(r.kind));
|
||
|
});
|
||
|
}
|
||
|
return [];
|
||
|
}
|
||
|
}
|
||
|
exports.FoldingRangeFeature = FoldingRangeFeature;
|