UNPKG

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