yaml-language-server
Version:
236 lines • 11.5 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
const sinon = __importStar(require("sinon"));
const sinon_chai_1 = __importDefault(require("sinon-chai"));
const chai = __importStar(require("chai"));
const yamlCodeLens_1 = require("../src/languageservice/services/yamlCodeLens");
const yamlSchemaService_1 = require("../src/languageservice/services/yamlSchemaService");
const testHelper_1 = require("./utils/testHelper");
const vscode_languageserver_protocol_1 = require("vscode-languageserver-protocol");
const commands_1 = require("../src/commands");
const telemetry_1 = require("../src/languageserver/telemetry");
const languageHandlers_1 = require("../src/languageserver/handlers/languageHandlers");
const yamlSettings_1 = require("../src/yamlSettings");
const expect = chai.expect;
chai.use(sinon_chai_1.default);
describe('YAML CodeLens', () => {
const sandbox = sinon.createSandbox();
let yamlSchemaService;
let telemetryStub;
let telemetry;
beforeEach(() => {
yamlSchemaService = sandbox.createStubInstance(yamlSchemaService_1.YAMLSchemaService);
telemetryStub = sandbox.createStubInstance(telemetry_1.TelemetryImpl);
telemetry = telemetryStub;
});
afterEach(() => {
sandbox.restore();
});
function createCommand(title, command, arg) {
return {
title,
command,
arguments: [arg],
};
}
function createCodeLens(title, command, arg) {
const lens = vscode_languageserver_protocol_1.CodeLens.create(vscode_languageserver_protocol_1.Range.create(0, 0, 0, 0));
lens.command = createCommand(title, command, arg);
return lens;
}
function createResolvedSchema(schema) {
return { schema };
}
it('should provides CodeLens with jumpToSchema command', async () => {
const doc = (0, testHelper_1.setupTextDocument)('foo: bar');
const schema = {
url: 'some://url/to/schema.json',
};
yamlSchemaService.getSchemaForResource.resolves(createResolvedSchema(schema));
const codeLens = new yamlCodeLens_1.YamlCodeLens(yamlSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result).is.not.empty;
expect(result[0].command).is.not.undefined;
expect(result[0].command).is.deep.equal(createCommand('schema.json', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json'));
});
it('should place CodeLens at beginning of the file and it has command', async () => {
const doc = (0, testHelper_1.setupTextDocument)('foo: bar');
const schema = {
url: 'some://url/to/schema.json',
};
yamlSchemaService.getSchemaForResource.resolves(createResolvedSchema(schema));
const codeLens = new yamlCodeLens_1.YamlCodeLens(yamlSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result[0].range).is.deep.equal(vscode_languageserver_protocol_1.Range.create(0, 0, 0, 0));
expect(result[0].command).is.deep.equal(createCommand('schema.json', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json'));
});
it('should wait for configuration update before providing CodeLens', async () => {
const doc = (0, testHelper_1.setupTextDocument)('foo: bar');
const expected = createCodeLens('schema.json', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json');
const yamlSettings = new yamlSettings_1.SettingsState();
yamlSettings.documents = new yamlSettings_1.TextDocumentTestManager();
yamlSettings.documents.set(doc);
let resolveConfiguration = () => undefined;
yamlSettings.configurationPullPromise = new Promise((resolve) => {
resolveConfiguration = resolve;
});
const getCodeLensStub = sandbox.stub().returns([expected]);
const languageService = {
getCodeLens: getCodeLensStub,
};
const codeLensHandler = new languageHandlers_1.LanguageHandlers({}, languageService, yamlSettings, {});
const response = Promise.resolve(codeLensHandler.codeLensHandler({
textDocument: { uri: doc.uri },
}));
let settled = false;
response.then(() => {
settled = true;
});
await Promise.resolve();
expect(settled).to.be.false;
expect(getCodeLensStub).not.called;
resolveConfiguration();
const result = await response;
expect(getCodeLensStub).calledOnceWithExactly(doc);
expect(result).deep.equal([expected]);
});
it('should place one CodeLens at beginning of the file for multiple documents', async () => {
const doc = (0, testHelper_1.setupTextDocument)('foo: bar\n---\nfoo: bar');
const schema = {
url: 'some://url/to/schema.json',
};
yamlSchemaService.getSchemaForResource.resolves(createResolvedSchema(schema));
const codeLens = new yamlCodeLens_1.YamlCodeLens(yamlSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result.length).to.eq(1);
expect(result[0].range).is.deep.equal(vscode_languageserver_protocol_1.Range.create(0, 0, 0, 0));
expect(result[0].command).is.deep.equal(createCommand('schema.json', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json'));
});
it('command name should contains schema title', async () => {
const doc = (0, testHelper_1.setupTextDocument)('foo: bar');
const schema = {
url: 'some://url/to/schema.json',
title: 'fooBar',
};
yamlSchemaService.getSchemaForResource.resolves(createResolvedSchema(schema));
const codeLens = new yamlCodeLens_1.YamlCodeLens(yamlSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result[0].command).is.deep.equal(createCommand('fooBar (schema.json)', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json'));
});
it('command name should contains schema title and description', async () => {
const doc = (0, testHelper_1.setupTextDocument)('foo: bar');
const schema = {
url: 'some://url/to/schema.json',
title: 'fooBar',
description: 'fooBarDescription',
};
yamlSchemaService.getSchemaForResource.resolves(createResolvedSchema(schema));
const codeLens = new yamlCodeLens_1.YamlCodeLens(yamlSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result[0].command).is.deep.equal(createCommand('fooBar - fooBarDescription (schema.json)', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json'));
});
it('should provide lens for oneOf schemas', async () => {
const doc = (0, testHelper_1.setupTextDocument)('foo: bar');
const schema = {
oneOf: [
{
url: 'some://url/schema1.json',
},
{
url: 'some://url/schema2.json',
},
],
};
yamlSchemaService.getSchemaForResource.resolves(createResolvedSchema(schema));
const codeLens = new yamlCodeLens_1.YamlCodeLens(yamlSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result).has.length(2);
expect(result).is.deep.equal([
createCodeLens('schema1.json', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema1.json'),
createCodeLens('schema2.json', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema2.json'),
]);
});
it('should provide lens for allOf schemas', async () => {
const doc = (0, testHelper_1.setupTextDocument)('foo: bar');
const schema = {
allOf: [
{
url: 'some://url/schema1.json',
},
{
url: 'some://url/schema2.json',
},
],
};
yamlSchemaService.getSchemaForResource.resolves(createResolvedSchema(schema));
const codeLens = new yamlCodeLens_1.YamlCodeLens(yamlSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result).has.length(2);
expect(result).is.deep.equal([
createCodeLens('schema1.json', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema1.json'),
createCodeLens('schema2.json', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema2.json'),
]);
});
it('should provide lens for anyOf schemas', async () => {
const doc = (0, testHelper_1.setupTextDocument)('foo: bar');
const schema = {
anyOf: [
{
url: 'some://url/schema1.json',
},
{
url: 'some://url/schema2.json',
},
],
};
yamlSchemaService.getSchemaForResource.resolves(createResolvedSchema(schema));
const codeLens = new yamlCodeLens_1.YamlCodeLens(yamlSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result).has.length(2);
expect(result).is.deep.equal([
createCodeLens('schema1.json', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema1.json'),
createCodeLens('schema2.json', commands_1.YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema2.json'),
]);
});
});
//# sourceMappingURL=yamlCodeLens.test.js.map