vscode-texinfo/src/contexts/document_symbol.ts

71 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-10-20 20:12:33 +00:00
/**
2020-10-25 18:14:13 +00:00
* contexts/document_symbol.ts
2021-03-16 12:01:13 +00:00
*
* Copyright (C) 2020,2021 CismonX <admin@cismon.net>
*
* This file is part of vscode-texinfo.
*
* vscode-texinfo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* vscode-texinfo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* vscode-texinfo. If not, see <https://www.gnu.org/licenses/>.
2020-10-20 20:12:33 +00:00
*/
import * as vscode from 'vscode';
2020-10-24 21:45:32 +00:00
import DocumentContext from './document';
import { lineNumToRange } from '../utils/misc';
import { FoldingRange, Optional } from '../utils/types';
2020-10-20 20:12:33 +00:00
/**
* Context for symbols in a Texinfo document.
*/
2020-10-24 21:45:32 +00:00
export default class DocumentSymbolContext {
2020-10-20 20:12:33 +00:00
private document = this.documentContext.document;
2020-10-25 17:52:13 +00:00
private documentSymbols?: vscode.DocumentSymbol[];
2020-10-20 20:12:33 +00:00
get values() {
2020-10-25 17:52:13 +00:00
return this.documentSymbols ??= this.calculcateDocumentSymbols();
2020-10-20 20:12:33 +00:00
}
2020-10-24 15:50:20 +00:00
clear() {
2020-10-25 17:52:13 +00:00
this.documentSymbols = undefined;
2020-10-20 20:12:33 +00:00
}
2020-10-24 21:45:32 +00:00
constructor(private readonly documentContext: DocumentContext) {}
2020-10-20 20:12:33 +00:00
/**
2020-10-24 15:50:20 +00:00
* Calculate document symbols based on folding ranges.
2020-10-20 20:12:33 +00:00
*/
private calculcateDocumentSymbols() {
2020-11-04 06:42:31 +00:00
const ranges = Array<Optional<FoldingRange>>(this.document.lineCount);
2020-10-26 17:28:11 +00:00
this.documentContext.foldingRange.values.forEach(range => range.kind ?? (ranges[range.start] = range));
return foldingRangeToSymbols(ranges, 0, ranges.length);
2020-10-20 20:12:33 +00:00
}
}
2020-11-04 06:42:31 +00:00
function foldingRangeToSymbols(ranges: readonly Optional<FoldingRange>[], start: number, end: number) {
2020-10-24 15:50:20 +00:00
const symbols = <vscode.DocumentSymbol[]>[];
for (let idx = start; idx < end; ++idx) {
const node = ranges[idx];
if (node === undefined) continue;
2020-10-24 15:51:44 +00:00
const range = lineNumToRange(idx, node.end);
const selectionRange = lineNumToRange(idx);
2020-10-24 15:50:20 +00:00
const symbol = new vscode.DocumentSymbol('@' + node.name, node.detail,
vscode.SymbolKind.String, range, selectionRange);
symbol.children = foldingRangeToSymbols(ranges, idx + 1, node.end);
symbols.push(symbol);
idx = node.end;
}
return symbols;
}