mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-28 01:25:39 -05:00
* Fix initial build breaks from 1.67 merge (#2514) * Update yarn lock files * Update build scripts * Fix tsconfig * Build breaks * WIP * Update yarn lock files * Misc breaks * Updates to package.json * Breaks * Update yarn * Fix breaks * Breaks * Build breaks * Breaks * Breaks * Breaks * Breaks * Breaks * Missing file * Breaks * Breaks * Breaks * Breaks * Breaks * Fix several runtime breaks (#2515) * Missing files * Runtime breaks * Fix proxy ordering issue * Remove commented code * Fix breaks with opening query editor * Fix post merge break * Updates related to setup build and other breaks (#2516) * Fix bundle build issues * Update distro * Fix distro merge and update build JS files * Disable pipeline steps * Remove stats call * Update license name * Make new RPM dependencies a warning * Fix extension manager version checks * Update JS file * Fix a few runtime breaks * Fixes * Fix runtime issues * Fix build breaks * Update notebook tests (part 1) * Fix broken tests * Linting errors * Fix hygiene * Disable lint rules * Bump distro * Turn off smoke tests * Disable integration tests * Remove failing "activate" test * Remove failed test assertion * Disable other broken test * Disable query history tests * Disable extension unit tests * Disable failing tasks
114 lines
3.8 KiB
TypeScript
114 lines
3.8 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
import Token = require('markdown-it/lib/token');
|
|
import * as vscode from 'vscode';
|
|
import { MarkdownEngine } from '../markdownEngine';
|
|
import { TableOfContents } from '../tableOfContents';
|
|
import { SkinnyTextDocument } from '../workspaceContents';
|
|
|
|
const rangeLimit = 5000;
|
|
|
|
interface MarkdownItTokenWithMap extends Token {
|
|
map: [number, number];
|
|
}
|
|
|
|
export class MdFoldingProvider implements vscode.FoldingRangeProvider {
|
|
|
|
constructor(
|
|
private readonly engine: MarkdownEngine
|
|
) { }
|
|
|
|
public async provideFoldingRanges(
|
|
document: SkinnyTextDocument,
|
|
_: vscode.FoldingContext,
|
|
_token: vscode.CancellationToken
|
|
): Promise<vscode.FoldingRange[]> {
|
|
const foldables = await Promise.all([
|
|
this.getRegions(document),
|
|
this.getHeaderFoldingRanges(document),
|
|
this.getBlockFoldingRanges(document)
|
|
]);
|
|
return foldables.flat().slice(0, rangeLimit);
|
|
}
|
|
|
|
private async getRegions(document: SkinnyTextDocument): Promise<vscode.FoldingRange[]> {
|
|
const tokens = await this.engine.parse(document);
|
|
const regionMarkers = tokens.filter(isRegionMarker)
|
|
.map(token => ({ line: token.map[0], isStart: isStartRegion(token.content) }));
|
|
|
|
const nestingStack: { line: number; isStart: boolean }[] = [];
|
|
return regionMarkers
|
|
.map(marker => {
|
|
if (marker.isStart) {
|
|
nestingStack.push(marker);
|
|
} else if (nestingStack.length && nestingStack[nestingStack.length - 1].isStart) {
|
|
return new vscode.FoldingRange(nestingStack.pop()!.line, marker.line, vscode.FoldingRangeKind.Region);
|
|
} else {
|
|
// noop: invalid nesting (i.e. [end, start] or [start, end, end])
|
|
}
|
|
return null;
|
|
})
|
|
.filter((region: vscode.FoldingRange | null): region is vscode.FoldingRange => !!region);
|
|
}
|
|
|
|
private async getHeaderFoldingRanges(document: SkinnyTextDocument) {
|
|
const toc = await TableOfContents.create(this.engine, document);
|
|
return toc.entries.map(entry => {
|
|
let endLine = entry.sectionLocation.range.end.line;
|
|
if (document.lineAt(endLine).isEmptyOrWhitespace && endLine >= entry.line + 1) {
|
|
endLine = endLine - 1;
|
|
}
|
|
return new vscode.FoldingRange(entry.line, endLine);
|
|
});
|
|
}
|
|
|
|
private async getBlockFoldingRanges(document: SkinnyTextDocument): Promise<vscode.FoldingRange[]> {
|
|
const tokens = await this.engine.parse(document);
|
|
const multiLineListItems = tokens.filter(isFoldableToken);
|
|
return multiLineListItems.map(listItem => {
|
|
const start = listItem.map[0];
|
|
let end = listItem.map[1] - 1;
|
|
if (document.lineAt(end).isEmptyOrWhitespace && end >= start + 1) {
|
|
end = end - 1;
|
|
}
|
|
return new vscode.FoldingRange(start, end, this.getFoldingRangeKind(listItem));
|
|
});
|
|
}
|
|
|
|
private getFoldingRangeKind(listItem: Token): vscode.FoldingRangeKind | undefined {
|
|
return listItem.type === 'html_block' && listItem.content.startsWith('<!--')
|
|
? vscode.FoldingRangeKind.Comment
|
|
: undefined;
|
|
}
|
|
}
|
|
|
|
const isStartRegion = (t: string) => /^\s*<!--\s*#?region\b.*-->/.test(t);
|
|
const isEndRegion = (t: string) => /^\s*<!--\s*#?endregion\b.*-->/.test(t);
|
|
|
|
const isRegionMarker = (token: Token): token is MarkdownItTokenWithMap =>
|
|
!!token.map && token.type === 'html_block' && (isStartRegion(token.content) || isEndRegion(token.content));
|
|
|
|
const isFoldableToken = (token: Token): token is MarkdownItTokenWithMap => {
|
|
if (!token.map) {
|
|
return false;
|
|
}
|
|
|
|
switch (token.type) {
|
|
case 'fence':
|
|
case 'list_item_open':
|
|
return token.map[1] > token.map[0];
|
|
|
|
case 'html_block':
|
|
if (isRegionMarker(token)) {
|
|
return false;
|
|
}
|
|
return token.map[1] > token.map[0] + 1;
|
|
|
|
default:
|
|
return false;
|
|
}
|
|
};
|