yaml-language-server
Version:
59 lines • 3.32 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "vscode-languageserver-types", "../utils/textBuffer"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.doDocumentOnTypeFormatting = doDocumentOnTypeFormatting;
const vscode_languageserver_types_1 = require("vscode-languageserver-types");
const textBuffer_1 = require("../utils/textBuffer");
function doDocumentOnTypeFormatting(document, params) {
const { position } = params;
const tb = new textBuffer_1.TextBuffer(document);
if (params.ch === '\n') {
const previousLine = tb.getLineContent(position.line - 1);
if (previousLine.trimEnd().endsWith(':')) {
let expectedIndentationLength = previousLine.length - previousLine.trimStart().length + params.options.tabSize;
if (previousLine.trimStart().startsWith('-')) {
expectedIndentationLength += params.options.tabSize;
}
const currentLine = tb.getLineContent(position.line).replace('\r', '').replace('\n', '');
if (currentLine.trim().length !== 0) {
// non-space content, do nothing
return;
}
else if (currentLine.length === expectedIndentationLength) {
// already right; do nothing
return;
}
else if (currentLine.length < expectedIndentationLength) {
return [vscode_languageserver_types_1.TextEdit.insert(position, ' '.repeat(expectedIndentationLength - currentLine.length))];
}
else {
return [
vscode_languageserver_types_1.TextEdit.del(vscode_languageserver_types_1.Range.create(vscode_languageserver_types_1.Position.create(position.line, 0), vscode_languageserver_types_1.Position.create(position.line, currentLine.length - expectedIndentationLength))),
];
}
}
if (previousLine.trimEnd().endsWith('|')) {
return [vscode_languageserver_types_1.TextEdit.insert(position, ' '.repeat(params.options.tabSize))];
}
if (previousLine.includes(' - ') && !previousLine.includes(': ')) {
return [vscode_languageserver_types_1.TextEdit.insert(position, '- ')];
}
if (previousLine.includes(' - ') && previousLine.includes(': ')) {
return [vscode_languageserver_types_1.TextEdit.insert(position, ' ')];
}
}
}
});
//# sourceMappingURL=yamlOnTypeFormatting.js.map