UNPKG

2.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/* --------------------------------------------------------------------------------------------
4 * Copyright (c) Remy Suen. All rights reserved.
5 * Licensed under the MIT License. See License.txt in the project root for license information.
6 * ------------------------------------------------------------------------------------------ */
7const vscode_languageserver_types_1 = require("vscode-languageserver-types");
8const line_1 = require("./line");
9const util_1 = require("./util");
10class Comment extends line_1.Line {
11 constructor(document, range) {
12 super(document, range);
13 }
14 toString() {
15 const content = this.getContent();
16 if (content) {
17 return "# " + content;
18 }
19 return "#";
20 }
21 /**
22 * Returns the content of this comment. This excludes leading and
23 * trailing whitespace as well as the # symbol. If the comment only
24 * consists of whitespace, the empty string will be returned.
25 */
26 getContent() {
27 let range = this.getContentRange();
28 if (range === null) {
29 return "";
30 }
31 return this.document.getText().substring(this.document.offsetAt(range.start), this.document.offsetAt(range.end));
32 }
33 /**
34 * Returns a range that includes the content of the comment
35 * excluding any leading and trailing whitespace as well as the #
36 * symbol. May return null if the comment only consists of whitespace
37 * characters.
38 */
39 getContentRange() {
40 let range = this.getRange();
41 const startOffset = this.document.offsetAt(range.start);
42 let raw = this.document.getText().substring(startOffset, this.document.offsetAt(range.end));
43 let start = -1;
44 let end = -1;
45 // skip the first # symbol
46 for (let i = 1; i < raw.length; i++) {
47 if (!util_1.Util.isWhitespace(raw.charAt(i))) {
48 start = i;
49 break;
50 }
51 }
52 if (start === -1) {
53 return null;
54 }
55 // go backwards up to the first # symbol
56 for (let i = raw.length - 1; i >= 1; i--) {
57 if (!util_1.Util.isWhitespace(raw.charAt(i))) {
58 end = i + 1;
59 break;
60 }
61 }
62 return vscode_languageserver_types_1.Range.create(this.document.positionAt(startOffset + start), this.document.positionAt(startOffset + end));
63 }
64}
65exports.Comment = Comment;