Move smart select and folding to md language server (#154334)

* Move smart select and folding to md language server

Also fixes a few minor issues:

- Don't log to web console
- Remove convert code since it is no longer needed
- Use correct extension id

* bump cache

* Bump package version

Co-authored-by: João Moreno <joao.moreno@microsoft.com>
This commit is contained in:
Matt Bierner 2022-07-07 07:30:03 -07:00 committed by GitHub
parent f9711dfc35
commit 2fb56e9d62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 55 additions and 1401 deletions

View file

@ -1 +1 @@
2022-06-10T10:20:54.664Z
2022-07-07T05:37:58.279Z

View file

@ -5,14 +5,7 @@
import { ILogger } from 'vscode-markdown-languageservice';
class ConsoleLogger implements ILogger {
public verbose(title: string, message: string, data?: any): void {
this.appendLine(`[Verbose ${ConsoleLogger.now()}] ${title}: ${message}`);
if (data) {
this.appendLine(ConsoleLogger.data2String(data));
}
}
export class LogFunctionLogger implements ILogger {
private static now(): string {
const now = new Date();
@ -21,10 +14,6 @@ class ConsoleLogger implements ILogger {
+ ':' + String(now.getUTCSeconds()).padStart(2, '0') + '.' + String(now.getMilliseconds()).padStart(3, '0');
}
private appendLine(value: string): void {
console.log(value);
}
private static data2String(data: any): string {
if (data instanceof Error) {
if (typeof data.stack === 'string') {
@ -37,6 +26,21 @@ class ConsoleLogger implements ILogger {
}
return JSON.stringify(data, undefined, 2);
}
constructor(
private readonly _logFn: typeof console.log
) { }
public verbose(title: string, message: string, data?: any): void {
this.appendLine(`[Verbose ${LogFunctionLogger.now()}] ${title}: ${message}`);
if (data) {
this.appendLine(LogFunctionLogger.data2String(data));
}
}
private appendLine(value: string): void {
this._logFn(value);
}
}
export const consoleLogger = new ConsoleLogger();
export const consoleLogger = new LogFunctionLogger(console.log);

View file

@ -5,10 +5,10 @@
import { Connection, Emitter, Event, InitializeParams, InitializeResult, RequestType, TextDocuments } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { DocumentSymbol, Position, Range } from 'vscode-languageserver-types';
import * as lsp from 'vscode-languageserver-types';
import * as md from 'vscode-markdown-languageservice';
import { URI } from 'vscode-uri';
import { consoleLogger } from './logging';
import { LogFunctionLogger } from './logging';
const parseRequestType: RequestType<{ uri: string }, md.Token[], any> = new RequestType('markdown/parse');
@ -31,8 +31,7 @@ class TextDocumentToITextDocumentAdapter implements md.ITextDocument {
}
positionAt(offset: number): md.IPosition {
const pos = this._doc.positionAt(offset);
return md.makePosition(pos.line, pos.character);
return this._doc.positionAt(offset);
}
}
@ -44,6 +43,8 @@ export function startServer(connection: Connection) {
return {
capabilities: {
documentSymbolProvider: true,
foldingRangeProvider: true,
selectionRangeProvider: true,
}
};
});
@ -85,15 +86,38 @@ export function startServer(connection: Connection) {
}
};
const provider = md.createLanguageService(workspace, parser, consoleLogger);
const logger = new LogFunctionLogger(connection.console.log.bind(connection.console));
const provider = md.createLanguageService(workspace, parser, logger);
connection.onDocumentSymbol(async (documentSymbolParams, _token): Promise<DocumentSymbol[]> => {
connection.onDocumentSymbol(async (params, token): Promise<lsp.DocumentSymbol[]> => {
try {
const document = documents.get(documentSymbolParams.textDocument.uri) as TextDocument | undefined;
const document = documents.get(params.textDocument.uri);
if (document) {
const response = await provider.provideDocumentSymbols(new TextDocumentToITextDocumentAdapter(document));
// TODO: only required because extra methods returned on positions/ranges
return response.map(symbol => convertDocumentSymbol(symbol));
return await provider.provideDocumentSymbols(new TextDocumentToITextDocumentAdapter(document), token);
}
} catch (e) {
console.error(e.stack);
}
return [];
});
connection.onFoldingRanges(async (params, token): Promise<lsp.FoldingRange[]> => {
try {
const document = documents.get(params.textDocument.uri);
if (document) {
return await provider.provideFoldingRanges(new TextDocumentToITextDocumentAdapter(document), token);
}
} catch (e) {
console.error(e.stack);
}
return [];
});
connection.onSelectionRanges(async (params, token): Promise<lsp.SelectionRange[] | undefined> => {
try {
const document = documents.get(params.textDocument.uri);
if (document) {
return await provider.provideSelectionRanges(new TextDocumentToITextDocumentAdapter(document), params.positions, token);
}
} catch (e) {
console.error(e.stack);
@ -103,30 +127,3 @@ export function startServer(connection: Connection) {
connection.listen();
}
function convertDocumentSymbol(sym: DocumentSymbol): DocumentSymbol {
return {
kind: sym.kind,
name: sym.name,
range: convertRange(sym.range),
selectionRange: convertRange(sym.selectionRange),
children: sym.children?.map(convertDocumentSymbol),
detail: sym.detail,
tags: sym.tags,
};
}
function convertRange(range: Range): Range {
return {
start: convertPosition(range.start),
end: convertPosition(range.end),
};
}
function convertPosition(start: Position): Position {
return {
character: start.character,
line: start.line,
};
}

View file

@ -43,8 +43,8 @@ vscode-languageserver@^8.0.2-next.4:
vscode-languageserver-protocol "3.17.2-next.6"
vscode-markdown-languageservice@mjbvz/vscode-markdown-languageservice:
version "1.0.0"
resolved "https://codeload.github.com/mjbvz/vscode-markdown-languageservice/tar.gz/e410b5df64659fbc186cf0a7a7c882c451e07b8b"
version "0.0.0-alpha.1"
resolved "https://codeload.github.com/mjbvz/vscode-markdown-languageservice/tar.gz/e1a0e00bf6a99cc543da64964cc0995537647d15"
dependencies:
vscode-languageserver-types "^3.17.1"
vscode-uri "^3.0.3"

View file

@ -30,7 +30,7 @@ export function activate(context: vscode.ExtensionContext) {
}
async function startServer(context: vscode.ExtensionContext, workspace: IMdWorkspace, parser: IMdParser): Promise<void> {
const clientMain = vscode.extensions.getExtension('vscode.css-language-features')?.packageJSON?.main || '';
const clientMain = vscode.extensions.getExtension('vscode.markdown-language-features')?.packageJSON?.main || '';
const serverMain = `./server/${clientMain.indexOf('/dist/') !== -1 ? 'dist' : 'out'}/node/main`;
const serverModule = context.asAbsolutePath(serverMain);

View file

@ -13,11 +13,9 @@ import { MdLinkProvider, registerDocumentLinkSupport } from './languageFeatures/
import { MdDocumentSymbolProvider } from './languageFeatures/documentSymbols';
import { registerDropIntoEditorSupport } from './languageFeatures/dropIntoEditor';
import { registerFindFileReferenceSupport } from './languageFeatures/fileReferences';
import { registerFoldingSupport } from './languageFeatures/folding';
import { registerPathCompletionSupport } from './languageFeatures/pathCompletions';
import { MdReferencesProvider, registerReferencesSupport } from './languageFeatures/references';
import { registerRenameSupport } from './languageFeatures/rename';
import { registerSmartSelectSupport } from './languageFeatures/smartSelect';
import { registerWorkspaceSymbolSupport } from './languageFeatures/workspaceSymbols';
import { ILogger } from './logging';
import { IMdParser, MarkdownItEngine, MdParsingProvider } from './markdownEngine';
@ -81,12 +79,10 @@ function registerMarkdownLanguageFeatures(
registerDocumentLinkSupport(selector, linkProvider),
registerDropIntoEditorSupport(selector),
registerFindFileReferenceSupport(commandManager, referencesProvider),
registerFoldingSupport(selector, parser, tocProvider),
registerPasteSupport(selector),
registerPathCompletionSupport(selector, workspace, parser, linkProvider),
registerReferencesSupport(selector, referencesProvider),
registerRenameSupport(selector, workspace, referencesProvider, parser.slugifier),
registerSmartSelectSupport(selector, parser, tocProvider),
registerWorkspaceSymbolSupport(workspace, symbolProvider),
);
}

View file

@ -1,123 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type Token = require('markdown-it/lib/token');
import * as vscode from 'vscode';
import { IMdParser } from '../markdownEngine';
import { MdTableOfContentsProvider } from '../tableOfContents';
import { getLine, ITextDocument } from '../types/textDocument';
import { isEmptyOrWhitespace } from '../util/string';
const rangeLimit = 5000;
interface MarkdownItTokenWithMap extends Token {
map: [number, number];
}
export class MdFoldingProvider implements vscode.FoldingRangeProvider {
constructor(
private readonly parser: IMdParser,
private readonly tocProvide: MdTableOfContentsProvider,
) { }
public async provideFoldingRanges(
document: ITextDocument,
_: 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: ITextDocument): Promise<vscode.FoldingRange[]> {
const tokens = await this.parser.tokenize(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: ITextDocument): Promise<vscode.FoldingRange[]> {
const toc = await this.tocProvide.getForDocument(document);
return toc.entries.map(entry => {
let endLine = entry.sectionLocation.range.end.line;
if (isEmptyOrWhitespace(getLine(document, endLine)) && endLine >= entry.line + 1) {
endLine = endLine - 1;
}
return new vscode.FoldingRange(entry.line, endLine);
});
}
private async getBlockFoldingRanges(document: ITextDocument): Promise<vscode.FoldingRange[]> {
const tokens = await this.parser.tokenize(document);
const multiLineListItems = tokens.filter(isFoldableToken);
return multiLineListItems.map(listItem => {
const start = listItem.map[0];
let end = listItem.map[1] - 1;
if (isEmptyOrWhitespace(getLine(document, end)) && 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;
}
};
export function registerFoldingSupport(
selector: vscode.DocumentSelector,
parser: IMdParser,
tocProvider: MdTableOfContentsProvider,
): vscode.Disposable {
return vscode.languages.registerFoldingRangeProvider(selector, new MdFoldingProvider(parser, tocProvider));
}

View file

@ -1,259 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import Token = require('markdown-it/lib/token');
import * as vscode from 'vscode';
import { IMdParser } from '../markdownEngine';
import { MdTableOfContentsProvider, TocEntry } from '../tableOfContents';
import { getLine, ITextDocument } from '../types/textDocument';
import { isEmptyOrWhitespace } from '../util/string';
interface MarkdownItTokenWithMap extends Token {
map: [number, number];
}
export class MdSmartSelect implements vscode.SelectionRangeProvider {
constructor(
private readonly parser: IMdParser,
private readonly tocProvider: MdTableOfContentsProvider,
) { }
public async provideSelectionRanges(document: ITextDocument, positions: vscode.Position[], _token: vscode.CancellationToken): Promise<vscode.SelectionRange[] | undefined> {
const promises = await Promise.all(positions.map((position) => {
return this.provideSelectionRange(document, position, _token);
}));
return promises.filter(item => item !== undefined) as vscode.SelectionRange[];
}
private async provideSelectionRange(document: ITextDocument, position: vscode.Position, _token: vscode.CancellationToken): Promise<vscode.SelectionRange | undefined> {
const headerRange = await this.getHeaderSelectionRange(document, position);
const blockRange = await this.getBlockSelectionRange(document, position, headerRange);
const inlineRange = await this.getInlineSelectionRange(document, position, blockRange);
return inlineRange || blockRange || headerRange;
}
private async getInlineSelectionRange(document: ITextDocument, position: vscode.Position, blockRange?: vscode.SelectionRange): Promise<vscode.SelectionRange | undefined> {
return createInlineRange(document, position, blockRange);
}
private async getBlockSelectionRange(document: ITextDocument, position: vscode.Position, headerRange?: vscode.SelectionRange): Promise<vscode.SelectionRange | undefined> {
const tokens = await this.parser.tokenize(document);
const blockTokens = getBlockTokensForPosition(tokens, position, headerRange);
if (blockTokens.length === 0) {
return undefined;
}
let currentRange: vscode.SelectionRange | undefined = headerRange ? headerRange : createBlockRange(blockTokens.shift()!, document, position.line);
for (let i = 0; i < blockTokens.length; i++) {
currentRange = createBlockRange(blockTokens[i], document, position.line, currentRange);
}
return currentRange;
}
private async getHeaderSelectionRange(document: ITextDocument, position: vscode.Position): Promise<vscode.SelectionRange | undefined> {
const toc = await this.tocProvider.getForDocument(document);
const headerInfo = getHeadersForPosition(toc.entries, position);
const headers = headerInfo.headers;
let currentRange: vscode.SelectionRange | undefined;
for (let i = 0; i < headers.length; i++) {
currentRange = createHeaderRange(headers[i], i === headers.length - 1, headerInfo.headerOnThisLine, currentRange, getFirstChildHeader(document, headers[i], toc.entries));
}
return currentRange;
}
}
function getHeadersForPosition(toc: readonly TocEntry[], position: vscode.Position): { headers: TocEntry[]; headerOnThisLine: boolean } {
const enclosingHeaders = toc.filter(header => header.sectionLocation.range.start.line <= position.line && header.sectionLocation.range.end.line >= position.line);
const sortedHeaders = enclosingHeaders.sort((header1, header2) => (header1.line - position.line) - (header2.line - position.line));
const onThisLine = toc.find(header => header.line === position.line) !== undefined;
return {
headers: sortedHeaders,
headerOnThisLine: onThisLine
};
}
function createHeaderRange(header: TocEntry, isClosestHeaderToPosition: boolean, onHeaderLine: boolean, parent?: vscode.SelectionRange, startOfChildRange?: vscode.Position): vscode.SelectionRange | undefined {
const range = header.sectionLocation.range;
const contentRange = new vscode.Range(range.start.translate(1), range.end);
if (onHeaderLine && isClosestHeaderToPosition && startOfChildRange) {
// selection was made on this header line, so select header and its content until the start of its first child
// then all of its content
return new vscode.SelectionRange(range.with(undefined, startOfChildRange), new vscode.SelectionRange(range, parent));
} else if (onHeaderLine && isClosestHeaderToPosition) {
// selection was made on this header line and no children so expand to all of its content
return new vscode.SelectionRange(range, parent);
} else if (isClosestHeaderToPosition && startOfChildRange) {
// selection was made within content and has child so select content
// of this header then all content then header
return new vscode.SelectionRange(contentRange.with(undefined, startOfChildRange), new vscode.SelectionRange(contentRange, (new vscode.SelectionRange(range, parent))));
} else {
// not on this header line so select content then header
return new vscode.SelectionRange(contentRange, new vscode.SelectionRange(range, parent));
}
}
function getBlockTokensForPosition(tokens: Token[], position: vscode.Position, parent?: vscode.SelectionRange): MarkdownItTokenWithMap[] {
const enclosingTokens = tokens.filter((token): token is MarkdownItTokenWithMap => !!token.map && (token.map[0] <= position.line && token.map[1] > position.line) && (!parent || (token.map[0] >= parent.range.start.line && token.map[1] <= parent.range.end.line + 1)) && isBlockElement(token));
if (enclosingTokens.length === 0) {
return [];
}
const sortedTokens = enclosingTokens.sort((token1, token2) => (token2.map[1] - token2.map[0]) - (token1.map[1] - token1.map[0]));
return sortedTokens;
}
function createBlockRange(block: MarkdownItTokenWithMap, document: ITextDocument, cursorLine: number, parent?: vscode.SelectionRange): vscode.SelectionRange | undefined {
if (block.type === 'fence') {
return createFencedRange(block, cursorLine, document, parent);
} else {
let startLine = isEmptyOrWhitespace(getLine(document, block.map[0])) ? block.map[0] + 1 : block.map[0];
let endLine = startLine === block.map[1] ? block.map[1] : block.map[1] - 1;
if (block.type === 'paragraph_open' && block.map[1] - block.map[0] === 2) {
startLine = endLine = cursorLine;
} else if (isList(block) && isEmptyOrWhitespace(getLine(document, endLine))) {
endLine = endLine - 1;
}
const range = new vscode.Range(startLine, 0, endLine, getLine(document, endLine).length);
if (parent?.range.contains(range) && !parent.range.isEqual(range)) {
return new vscode.SelectionRange(range, parent);
} else if (parent?.range.isEqual(range)) {
return parent;
} else {
return new vscode.SelectionRange(range);
}
}
}
function createInlineRange(document: ITextDocument, cursorPosition: vscode.Position, parent?: vscode.SelectionRange): vscode.SelectionRange | undefined {
const lineText = getLine(document, cursorPosition.line);
const boldSelection = createBoldRange(lineText, cursorPosition.character, cursorPosition.line, parent);
const italicSelection = createOtherInlineRange(lineText, cursorPosition.character, cursorPosition.line, true, parent);
let comboSelection: vscode.SelectionRange | undefined;
if (boldSelection && italicSelection && !boldSelection.range.isEqual(italicSelection.range)) {
if (boldSelection.range.contains(italicSelection.range)) {
comboSelection = createOtherInlineRange(lineText, cursorPosition.character, cursorPosition.line, true, boldSelection);
} else if (italicSelection.range.contains(boldSelection.range)) {
comboSelection = createBoldRange(lineText, cursorPosition.character, cursorPosition.line, italicSelection);
}
}
const linkSelection = createLinkRange(lineText, cursorPosition.character, cursorPosition.line, comboSelection || boldSelection || italicSelection || parent);
const inlineCodeBlockSelection = createOtherInlineRange(lineText, cursorPosition.character, cursorPosition.line, false, linkSelection || parent);
return inlineCodeBlockSelection || linkSelection || comboSelection || boldSelection || italicSelection;
}
function createFencedRange(token: MarkdownItTokenWithMap, cursorLine: number, document: ITextDocument, parent?: vscode.SelectionRange): vscode.SelectionRange {
const startLine = token.map[0];
const endLine = token.map[1] - 1;
const onFenceLine = cursorLine === startLine || cursorLine === endLine;
const fenceRange = new vscode.Range(startLine, 0, endLine, getLine(document, endLine).length);
const contentRange = endLine - startLine > 2 && !onFenceLine ? new vscode.Range(startLine + 1, 0, endLine - 1, getLine(document, endLine - 1).length) : undefined;
if (contentRange) {
return new vscode.SelectionRange(contentRange, new vscode.SelectionRange(fenceRange, parent));
} else {
if (parent?.range.isEqual(fenceRange)) {
return parent;
} else {
return new vscode.SelectionRange(fenceRange, parent);
}
}
}
function createBoldRange(lineText: string, cursorChar: number, cursorLine: number, parent?: vscode.SelectionRange): vscode.SelectionRange | undefined {
const regex = /\*\*([^*]+\*?[^*]+\*?[^*]+)\*\*/gim;
const matches = [...lineText.matchAll(regex)].filter(match => lineText.indexOf(match[0]) <= cursorChar && lineText.indexOf(match[0]) + match[0].length >= cursorChar);
if (matches.length) {
// should only be one match, so select first and index 0 contains the entire match
const bold = matches[0][0];
const startIndex = lineText.indexOf(bold);
const cursorOnStars = cursorChar === startIndex || cursorChar === startIndex + 1 || cursorChar === startIndex + bold.length || cursorChar === startIndex + bold.length - 1;
const contentAndStars = new vscode.SelectionRange(new vscode.Range(cursorLine, startIndex, cursorLine, startIndex + bold.length), parent);
const content = new vscode.SelectionRange(new vscode.Range(cursorLine, startIndex + 2, cursorLine, startIndex + bold.length - 2), contentAndStars);
return cursorOnStars ? contentAndStars : content;
}
return undefined;
}
function createOtherInlineRange(lineText: string, cursorChar: number, cursorLine: number, isItalic: boolean, parent?: vscode.SelectionRange): vscode.SelectionRange | undefined {
const italicRegexes = [/(?:[^*]+)(\*([^*]+)(?:\*\*[^*]*\*\*)*([^*]+)\*)(?:[^*]+)/g, /^(?:[^*]*)(\*([^*]+)(?:\*\*[^*]*\*\*)*([^*]+)\*)(?:[^*]*)$/g];
let matches = [];
if (isItalic) {
matches = [...lineText.matchAll(italicRegexes[0])].filter(match => lineText.indexOf(match[0]) <= cursorChar && lineText.indexOf(match[0]) + match[0].length >= cursorChar);
if (!matches.length) {
matches = [...lineText.matchAll(italicRegexes[1])].filter(match => lineText.indexOf(match[0]) <= cursorChar && lineText.indexOf(match[0]) + match[0].length >= cursorChar);
}
} else {
matches = [...lineText.matchAll(/\`[^\`]*\`/g)].filter(match => lineText.indexOf(match[0]) <= cursorChar && lineText.indexOf(match[0]) + match[0].length >= cursorChar);
}
if (matches.length) {
// should only be one match, so select first and select group 1 for italics because that contains just the italic section
// doesn't include the leading and trailing characters which are guaranteed to not be * so as not to be confused with bold
const match = isItalic ? matches[0][1] : matches[0][0];
const startIndex = lineText.indexOf(match);
const cursorOnType = cursorChar === startIndex || cursorChar === startIndex + match.length;
const contentAndType = new vscode.SelectionRange(new vscode.Range(cursorLine, startIndex, cursorLine, startIndex + match.length), parent);
const content = new vscode.SelectionRange(new vscode.Range(cursorLine, startIndex + 1, cursorLine, startIndex + match.length - 1), contentAndType);
return cursorOnType ? contentAndType : content;
}
return undefined;
}
function createLinkRange(lineText: string, cursorChar: number, cursorLine: number, parent?: vscode.SelectionRange): vscode.SelectionRange | undefined {
const regex = /(\[[^\(\)]*\])(\([^\[\]]*\))/g;
const matches = [...lineText.matchAll(regex)].filter(match => lineText.indexOf(match[0]) <= cursorChar && lineText.indexOf(match[0]) + match[0].length > cursorChar);
if (matches.length) {
// should only be one match, so select first and index 0 contains the entire match, so match = [text](url)
const link = matches[0][0];
const linkRange = new vscode.SelectionRange(new vscode.Range(cursorLine, lineText.indexOf(link), cursorLine, lineText.indexOf(link) + link.length), parent);
const linkText = matches[0][1];
const url = matches[0][2];
// determine if cursor is within [text] or (url) in order to know which should be selected
const nearestType = cursorChar >= lineText.indexOf(linkText) && cursorChar < lineText.indexOf(linkText) + linkText.length ? linkText : url;
const indexOfType = lineText.indexOf(nearestType);
// determine if cursor is on a bracket or paren and if so, return the [content] or (content), skipping over the content range
const cursorOnType = cursorChar === indexOfType || cursorChar === indexOfType + nearestType.length;
const contentAndNearestType = new vscode.SelectionRange(new vscode.Range(cursorLine, indexOfType, cursorLine, indexOfType + nearestType.length), linkRange);
const content = new vscode.SelectionRange(new vscode.Range(cursorLine, indexOfType + 1, cursorLine, indexOfType + nearestType.length - 1), contentAndNearestType);
return cursorOnType ? contentAndNearestType : content;
}
return undefined;
}
function isList(token: Token): boolean {
return token.type ? ['ordered_list_open', 'list_item_open', 'bullet_list_open'].includes(token.type) : false;
}
function isBlockElement(token: Token): boolean {
return !['list_item_close', 'paragraph_close', 'bullet_list_close', 'inline', 'heading_close', 'heading_open'].includes(token.type);
}
function getFirstChildHeader(document: ITextDocument, header?: TocEntry, toc?: readonly TocEntry[]): vscode.Position | undefined {
let childRange: vscode.Position | undefined;
if (header && toc) {
const children = toc.filter(t => header.sectionLocation.range.contains(t.sectionLocation.range) && t.sectionLocation.range.start.line > header.sectionLocation.range.start.line).sort((t1, t2) => t1.line - t2.line);
if (children.length > 0) {
childRange = children[0].sectionLocation.range.start;
const lineText = getLine(document, childRange.line - 1);
return childRange ? childRange.translate(-1, lineText.length) : undefined;
}
}
return undefined;
}
export function registerSmartSelectSupport(
selector: vscode.DocumentSelector,
parser: IMdParser,
tocProvider: MdTableOfContentsProvider,
): vscode.Disposable {
return vscode.languages.registerSelectionRangeProvider(selector, new MdSmartSelect(parser, tocProvider));
}

View file

@ -1,230 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import 'mocha';
import * as vscode from 'vscode';
import { MdFoldingProvider } from '../languageFeatures/folding';
import { MdTableOfContentsProvider } from '../tableOfContents';
import { noopToken } from '../util/cancellation';
import { DisposableStore } from '../util/dispose';
import { InMemoryDocument } from '../util/inMemoryDocument';
import { createNewMarkdownEngine } from './engine';
import { InMemoryMdWorkspace } from './inMemoryWorkspace';
import { nulLogger } from './nulLogging';
import { joinLines, withStore } from './util';
const testFileName = vscode.Uri.file('test.md');
async function getFoldsForDocument(store: DisposableStore, contents: string) {
const doc = new InMemoryDocument(testFileName, contents);
const workspace = store.add(new InMemoryMdWorkspace([doc]));
const engine = createNewMarkdownEngine();
const tocProvider = store.add(new MdTableOfContentsProvider(engine, workspace, nulLogger));
const provider = new MdFoldingProvider(engine, tocProvider);
return provider.provideFoldingRanges(doc, {}, noopToken);
}
suite('markdown.FoldingProvider', () => {
test('Should not return anything for empty document', withStore(async (store) => {
const folds = await getFoldsForDocument(store, ``);
assert.strictEqual(folds.length, 0);
}));
test('Should not return anything for document without headers', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
`a`,
`**b** afas`,
`a#b`,
`a`,
));
assert.strictEqual(folds.length, 0);
}));
test('Should fold from header to end of document', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
`a`,
`# b`,
`c`,
`d`,
));
assert.strictEqual(folds.length, 1);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 1);
assert.strictEqual(firstFold.end, 3);
}));
test('Should leave single newline before next header', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
``,
`# a`,
`x`,
``,
`# b`,
`y`,
));
assert.strictEqual(folds.length, 2);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 1);
assert.strictEqual(firstFold.end, 2);
}));
test('Should collapse multiple newlines to single newline before next header', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
``,
`# a`,
`x`,
``,
``,
``,
`# b`,
`y`
));
assert.strictEqual(folds.length, 2);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 1);
assert.strictEqual(firstFold.end, 4);
}));
test('Should not collapse if there is no newline before next header', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
``,
`# a`,
`x`,
`# b`,
`y`,
));
assert.strictEqual(folds.length, 2);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 1);
assert.strictEqual(firstFold.end, 2);
}));
test('Should fold nested <!-- #region --> markers', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
`a`,
`<!-- #region -->`,
`b`,
`<!-- #region hello!-->`,
`b.a`,
`<!-- #endregion -->`,
`b`,
`<!-- #region: foo! -->`,
`b.b`,
`<!-- #endregion: foo -->`,
`b`,
`<!-- #endregion -->`,
`a`,
));
assert.strictEqual(folds.length, 3);
const [outer, first, second] = folds.sort((a, b) => a.start - b.start);
assert.strictEqual(outer.start, 1);
assert.strictEqual(outer.end, 11);
assert.strictEqual(first.start, 3);
assert.strictEqual(first.end, 5);
assert.strictEqual(second.start, 7);
assert.strictEqual(second.end, 9);
}));
test('Should fold from list to end of document', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
`a`,
`- b`,
`c`,
`d`,
));
assert.strictEqual(folds.length, 1);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 1);
assert.strictEqual(firstFold.end, 3);
}));
test('lists folds should span multiple lines of content', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
`a`,
`- This list item\n spans multiple\n lines.`,
));
assert.strictEqual(folds.length, 1);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 1);
assert.strictEqual(firstFold.end, 3);
}));
test('List should leave single blankline before new element', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
`- a`,
`a`,
``,
``,
`b`
));
assert.strictEqual(folds.length, 1);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 0);
assert.strictEqual(firstFold.end, 2);
}));
test('Should fold fenced code blocks', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
`~~~ts`,
`a`,
`~~~`,
`b`,
));
assert.strictEqual(folds.length, 1);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 0);
assert.strictEqual(firstFold.end, 2);
}));
test('Should fold fenced code blocks with yaml front matter', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
`---`,
`title: bla`,
`---`,
``,
`~~~ts`,
`a`,
`~~~`,
``,
`a`,
`a`,
`b`,
`a`,
));
assert.strictEqual(folds.length, 1);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 4);
assert.strictEqual(firstFold.end, 6);
}));
test('Should fold html blocks', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
`x`,
`<div>`,
` fa`,
`</div>`,
));
assert.strictEqual(folds.length, 1);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 1);
assert.strictEqual(firstFold.end, 3);
}));
test('Should fold html block comments', withStore(async (store) => {
const folds = await getFoldsForDocument(store, joinLines(
`x`,
`<!--`,
`fa`,
`-->`
));
assert.strictEqual(folds.length, 1);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 1);
assert.strictEqual(firstFold.end, 3);
assert.strictEqual(firstFold.kind, vscode.FoldingRangeKind.Comment);
}));
});

View file

@ -1,731 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import * as vscode from 'vscode';
import { MdSmartSelect } from '../languageFeatures/smartSelect';
import { MdTableOfContentsProvider } from '../tableOfContents';
import { InMemoryDocument } from '../util/inMemoryDocument';
import { createNewMarkdownEngine } from './engine';
import { InMemoryMdWorkspace } from './inMemoryWorkspace';
import { nulLogger } from './nulLogging';
import { CURSOR, getCursorPositions, joinLines } from './util';
const testFileName = vscode.Uri.file('test.md');
suite('markdown.SmartSelect', () => {
test('Smart select single word', async () => {
const ranges = await getSelectionRangesForDocument(`Hel${CURSOR}lo`);
assertNestedLineNumbersEqual(ranges![0], [0, 0]);
});
test('Smart select multi-line paragraph', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`Many of the core components and extensions to ${CURSOR}VS Code live in their own repositories on GitHub. `,
`For example, the[node debug adapter](https://github.com/microsoft/vscode-node-debug) and the [mono debug adapter]`,
`(https://github.com/microsoft/vscode-mono-debug) have their own repositories. For a complete list, please visit the [Related Projects](https://github.com/microsoft/vscode/wiki/Related-Projects) page on our [wiki](https://github.com/microsoft/vscode/wiki).`
));
assertNestedLineNumbersEqual(ranges![0], [0, 2]);
});
test('Smart select paragraph', async () => {
const ranges = await getSelectionRangesForDocument(`Many of the core components and extensions to ${CURSOR}VS Code live in their own repositories on GitHub. For example, the [node debug adapter](https://github.com/microsoft/vscode-node-debug) and the [mono debug adapter](https://github.com/microsoft/vscode-mono-debug) have their own repositories. For a complete list, please visit the [Related Projects](https://github.com/microsoft/vscode/wiki/Related-Projects) page on our [wiki](https://github.com/microsoft/vscode/wiki).`);
assertNestedLineNumbersEqual(ranges![0], [0, 0]);
});
test('Smart select html block', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`<p align="center">`,
`${CURSOR}<img alt="VS Code in action" src="https://user-images.githubusercontent.com/1487073/58344409-70473b80-7e0a-11e9-8570-b2efc6f8fa44.png">`,
`</p>`));
assertNestedLineNumbersEqual(ranges![0], [0, 2]);
});
test('Smart select header on header line', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# Header${CURSOR}`,
`Hello`));
assertNestedLineNumbersEqual(ranges![0], [0, 1]);
});
test('Smart select single word w grandparent header on text line', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`## ParentHeader`,
`# Header`,
`${CURSOR}Hello`
));
assertNestedLineNumbersEqual(ranges![0], [2, 2], [1, 2]);
});
test('Smart select html block w parent header', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# Header`,
`${CURSOR}<p align="center">`,
`<img alt="VS Code in action" src="https://user-images.githubusercontent.com/1487073/58344409-70473b80-7e0a-11e9-8570-b2efc6f8fa44.png">`,
`</p>`));
assertNestedLineNumbersEqual(ranges![0], [1, 1], [1, 3], [0, 3]);
});
test('Smart select fenced code block', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`~~~`,
`a${CURSOR}`,
`~~~`));
assertNestedLineNumbersEqual(ranges![0], [0, 2]);
});
test('Smart select list', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`- item 1`,
`- ${CURSOR}item 2`,
`- item 3`,
`- item 4`));
assertNestedLineNumbersEqual(ranges![0], [1, 1], [0, 3]);
});
test('Smart select list with fenced code block', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`- item 1`,
`- ~~~`,
` ${CURSOR}a`,
` ~~~`,
`- item 3`,
`- item 4`));
assertNestedLineNumbersEqual(ranges![0], [1, 3], [0, 5]);
});
test('Smart select multi cursor', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`- ${CURSOR}item 1`,
`- ~~~`,
` a`,
` ~~~`,
`- ${CURSOR}item 3`,
`- item 4`));
assertNestedLineNumbersEqual(ranges![0], [0, 0], [0, 5]);
assertNestedLineNumbersEqual(ranges![1], [4, 4], [0, 5]);
});
test('Smart select nested block quotes', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`> item 1`,
`> item 2`,
`>> ${CURSOR}item 3`,
`>> item 4`));
assertNestedLineNumbersEqual(ranges![0], [2, 2], [2, 3], [0, 3]);
});
test('Smart select multi nested block quotes', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`> item 1`,
`>> item 2`,
`>>> ${CURSOR}item 3`,
`>>>> item 4`));
assertNestedLineNumbersEqual(ranges![0], [2, 2], [2, 3], [1, 3], [0, 3]);
});
test('Smart select subheader content', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
`content 1`,
`## sub header 1`,
`${CURSOR}content 2`,
`# main header 2`));
assertNestedLineNumbersEqual(ranges![0], [3, 3], [2, 3], [1, 3], [0, 3]);
});
test('Smart select subheader line', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
`content 1`,
`## sub header 1${CURSOR}`,
`content 2`,
`# main header 2`));
assertNestedLineNumbersEqual(ranges![0], [2, 3], [1, 3], [0, 3]);
});
test('Smart select blank line', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
`content 1`,
`${CURSOR} `,
`content 2`,
`# main header 2`));
assertNestedLineNumbersEqual(ranges![0], [1, 3], [0, 3]);
});
test('Smart select line between paragraphs', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`paragraph 1`,
`${CURSOR}`,
`paragraph 2`));
assertNestedLineNumbersEqual(ranges![0], [0, 2]);
});
test('Smart select empty document', async () => {
const ranges = await getSelectionRangesForDocument(``, [new vscode.Position(0, 0)]);
assert.strictEqual(ranges!.length, 0);
});
test('Smart select fenced code block then list then subheader content then subheader then header content then header', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
/* 00 */ `# main header 1`,
/* 01 */ `content 1`,
/* 02 */ `## sub header 1`,
/* 03 */ `- item 1`,
/* 04 */ `- ~~~`,
/* 05 */ ` ${CURSOR}a`,
/* 06 */ ` ~~~`,
/* 07 */ `- item 3`,
/* 08 */ `- item 4`,
/* 09 */ ``,
/* 10 */ `more content`,
/* 11 */ `# main header 2`));
assertNestedLineNumbersEqual(ranges![0], [4, 6], [3, 8], [3, 10], [2, 10], [1, 10], [0, 10]);
});
test('Smart select list with one element without selecting child subheader', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
/* 00 */ `# main header 1`,
/* 01 */ ``,
/* 02 */ `- list ${CURSOR}`,
/* 03 */ ``,
/* 04 */ `## sub header`,
/* 05 */ ``,
/* 06 */ `content 2`,
/* 07 */ `# main header 2`));
assertNestedLineNumbersEqual(ranges![0], [2, 2], [1, 3], [1, 6], [0, 6]);
});
test('Smart select content under header then subheaders and their content', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main ${CURSOR}header 1`,
``,
`- list`,
`paragraph`,
`## sub header`,
``,
`content 2`,
`# main header 2`));
assertNestedLineNumbersEqual(ranges![0], [0, 3], [0, 6]);
});
test('Smart select last blockquote element under header then subheaders and their content', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
``,
`> block`,
`> block`,
`>> block`,
`>> ${CURSOR}block`,
``,
`paragraph`,
`## sub header`,
``,
`content 2`,
`# main header 2`));
assertNestedLineNumbersEqual(ranges![0], [5, 5], [4, 5], [2, 5], [1, 7], [1, 10], [0, 10]);
});
test('Smart select content of subheader then subheader then content of main header then main header', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
``,
`> block`,
`> block`,
`>> block`,
`>> block`,
``,
`paragraph`,
`## sub header`,
``,
``,
`${CURSOR}`,
``,
`### main header 2`,
`- content 2`,
`- content 2`,
`- content 2`,
`content 2`));
assertNestedLineNumbersEqual(ranges![0], [11, 11], [9, 12], [9, 17], [8, 17], [1, 17], [0, 17]);
});
test('Smart select last line content of subheader then subheader then content of main header then main header', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
``,
`> block`,
`> block`,
`>> block`,
`>> block`,
``,
`paragraph`,
`## sub header`,
``,
``,
``,
``,
`### main header 2`,
`- content 2`,
`- content 2`,
`- content 2`,
`- ${CURSOR}content 2`));
assertNestedLineNumbersEqual(ranges![0], [17, 17], [14, 17], [13, 17], [9, 17], [8, 17], [1, 17], [0, 17]);
});
test('Smart select last line content after content of subheader then subheader then content of main header then main header', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
``,
`> block`,
`> block`,
`>> block`,
`>> block`,
``,
`paragraph`,
`## sub header`,
``,
``,
``,
``,
`### main header 2`,
`- content 2`,
`- content 2`,
`- content 2`,
`- content 2${CURSOR}`));
assertNestedLineNumbersEqual(ranges![0], [17, 17], [14, 17], [13, 17], [9, 17], [8, 17], [1, 17], [0, 17]);
});
test('Smart select fenced code block then list then rest of content', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
``,
`> block`,
`> block`,
`>> block`,
`>> block`,
``,
`- paragraph`,
`- ~~~`,
` my`,
` ${CURSOR}code`,
` goes here`,
` ~~~`,
`- content`,
`- content 2`,
`- content 2`,
`- content 2`,
`- content 2`));
assertNestedLineNumbersEqual(ranges![0], [9, 11], [8, 12], [8, 12], [7, 17], [1, 17], [0, 17]);
});
test('Smart select fenced code block then list then rest of content on fenced line', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
``,
`> block`,
`> block`,
`>> block`,
`>> block`,
``,
`- paragraph`,
`- ~~~${CURSOR}`,
` my`,
` code`,
` goes here`,
` ~~~`,
`- content`,
`- content 2`,
`- content 2`,
`- content 2`,
`- content 2`));
assertNestedLineNumbersEqual(ranges![0], [8, 12], [7, 17], [1, 17], [0, 17]);
});
test('Smart select without multiple ranges', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
``,
``,
`- ${CURSOR}paragraph`,
`- content`));
assertNestedLineNumbersEqual(ranges![0], [3, 3], [3, 4], [1, 4], [0, 4]);
});
test('Smart select on second level of a list', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`* level 0`,
` * level 1`,
` * level 1`,
` * level 2`,
` * level 1`,
` * level ${CURSOR}1`,
`* level 0`));
assertNestedLineNumbersEqual(ranges![0], [5, 5], [1, 5], [0, 5], [0, 6]);
});
test('Smart select on third level of a list', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`* level 0`,
` * level 1`,
` * level 1`,
` * level ${CURSOR}2`,
` * level 2`,
` * level 1`,
` * level 1`,
`* level 0`));
assertNestedLineNumbersEqual(ranges![0], [3, 3], [3, 4], [2, 4], [1, 6], [0, 6], [0, 7]);
});
test('Smart select level 2 then level 1', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`* level 1`,
` * level ${CURSOR}2`,
` * level 2`,
`* level 1`));
assertNestedLineNumbersEqual(ranges![0], [1, 1], [1, 2], [0, 2], [0, 3]);
});
test('Smart select last list item', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`- level 1`,
`- level 2`,
`- level 2`,
`- level ${CURSOR}1`));
assertNestedLineNumbersEqual(ranges![0], [3, 3], [0, 3]);
});
test('Smart select without multiple ranges', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
``,
``,
`- ${CURSOR}paragraph`,
`- content`));
assertNestedLineNumbersEqual(ranges![0], [3, 3], [3, 4], [1, 4], [0, 4]);
});
test('Smart select on second level of a list', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`* level 0`,
` * level 1`,
` * level 1`,
` * level 2`,
` * level 1`,
` * level ${CURSOR}1`,
`* level 0`));
assertNestedLineNumbersEqual(ranges![0], [5, 5], [1, 5], [0, 5], [0, 6]);
});
test('Smart select on third level of a list', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`* level 0`,
` * level 1`,
` * level 1`,
` * level ${CURSOR}2`,
` * level 2`,
` * level 1`,
` * level 1`,
`* level 0`));
assertNestedLineNumbersEqual(ranges![0], [3, 3], [3, 4], [2, 4], [1, 6], [0, 6], [0, 7]);
});
test('Smart select level 2 then level 1', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`* level 1`,
` * level ${CURSOR}2`,
` * level 2`,
`* level 1`));
assertNestedLineNumbersEqual(ranges![0], [1, 1], [1, 2], [0, 2], [0, 3]);
});
test('Smart select bold', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`stuff here **new${CURSOR}item** and here`
));
assertNestedRangesEqual(ranges![0], [0, 13, 0, 30], [0, 11, 0, 32], [0, 0, 0, 41]);
});
test('Smart select link', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`stuff here [text](https${CURSOR}://google.com) and here`
));
assertNestedRangesEqual(ranges![0], [0, 18, 0, 46], [0, 17, 0, 47], [0, 11, 0, 47], [0, 0, 0, 56]);
});
test('Smart select brackets', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`stuff here [te${CURSOR}xt](https://google.com) and here`
));
assertNestedRangesEqual(ranges![0], [0, 12, 0, 26], [0, 11, 0, 27], [0, 11, 0, 47], [0, 0, 0, 56]);
});
test('Smart select brackets under header in list', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
``,
`- list`,
`paragraph`,
`## sub header`,
`- list`,
`- stuff here [te${CURSOR}xt](https://google.com) and here`,
`- list`
));
assertNestedRangesEqual(ranges![0], [6, 14, 6, 28], [6, 13, 6, 29], [6, 13, 6, 49], [6, 0, 6, 58], [5, 0, 7, 6], [4, 0, 7, 6], [1, 0, 7, 6], [0, 0, 7, 6]);
});
test('Smart select link under header in list', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
``,
`- list`,
`paragraph`,
`## sub header`,
`- list`,
`- stuff here [text](${CURSOR}https://google.com) and here`,
`- list`
));
assertNestedRangesEqual(ranges![0], [6, 20, 6, 48], [6, 19, 6, 49], [6, 13, 6, 49], [6, 0, 6, 58], [5, 0, 7, 6], [4, 0, 7, 6], [1, 0, 7, 6], [0, 0, 7, 6]);
});
test('Smart select bold within list where multiple bold elements exists', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`# main header 1`,
``,
`- list`,
`paragraph`,
`## sub header`,
`- list`,
`- stuff here [text] **${CURSOR}items in here** and **here**`,
`- list`
));
assertNestedRangesEqual(ranges![0], [6, 22, 6, 45], [6, 20, 6, 47], [6, 0, 6, 60], [5, 0, 7, 6], [4, 0, 7, 6], [1, 0, 7, 6], [0, 0, 7, 6]);
});
test('Smart select link in paragraph with multiple links', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`This[extension](https://marketplace.visualstudio.com/items?itemName=meganrogge.template-string-converter) addresses this [requ${CURSOR}est](https://github.com/microsoft/vscode/issues/56704) to convert Javascript/Typescript quotes to backticks when has been entered within a string.`
));
assertNestedRangesEqual(ranges![0], [0, 123, 0, 140], [0, 122, 0, 141], [0, 122, 0, 191], [0, 0, 0, 283]);
});
test('Smart select bold link', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`**[extens${CURSOR}ion](https://google.com)**`
));
assertNestedRangesEqual(ranges![0], [0, 3, 0, 22], [0, 2, 0, 23], [0, 2, 0, 43], [0, 2, 0, 43], [0, 0, 0, 45], [0, 0, 0, 45]);
});
test('Smart select inline code block', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`[\`code ${CURSOR} link\`]`
));
assertNestedRangesEqual(ranges![0], [0, 2, 0, 22], [0, 1, 0, 23], [0, 0, 0, 24]);
});
test('Smart select link with inline code block text', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`[\`code ${CURSOR} link\`](http://example.com)`
));
assertNestedRangesEqual(ranges![0], [0, 2, 0, 22], [0, 1, 0, 23], [0, 1, 0, 23], [0, 0, 0, 24], [0, 0, 0, 44], [0, 0, 0, 44]);
});
test('Smart select italic', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`*some nice ${CURSOR}text*`
));
assertNestedRangesEqual(ranges![0], [0, 1, 0, 25], [0, 0, 0, 26], [0, 0, 0, 26]);
});
test('Smart select italic link', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`*[extens${CURSOR}ion](https://google.com)*`
));
assertNestedRangesEqual(ranges![0], [0, 2, 0, 21], [0, 1, 0, 22], [0, 1, 0, 42], [0, 1, 0, 42], [0, 0, 0, 43], [0, 0, 0, 43]);
});
test('Smart select italic on end', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`*word1 word2 word3${CURSOR}*`
));
assertNestedRangesEqual(ranges![0], [0, 1, 0, 28], [0, 0, 0, 29], [0, 0, 0, 29]);
});
test('Smart select italic then bold', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`outer text **bold words *italic ${CURSOR} words* bold words** outer text`
));
assertNestedRangesEqual(ranges![0], [0, 25, 0, 48], [0, 24, 0, 49], [0, 13, 0, 60], [0, 11, 0, 62], [0, 0, 0, 73]);
});
test('Smart select bold then italic', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`outer text *italic words **bold ${CURSOR} words** italic words* outer text`
));
assertNestedRangesEqual(ranges![0], [0, 27, 0, 48], [0, 25, 0, 50], [0, 12, 0, 63], [0, 11, 0, 64], [0, 0, 0, 75]);
});
test('Third level header from release notes', async () => {
const ranges = await getSelectionRangesForDocument(
joinLines(
`---`,
`Order: 60`,
`TOCTitle: October 2020`,
`PageTitle: Visual Studio Code October 2020`,
`MetaDescription: Learn what is new in the Visual Studio Code October 2020 Release (1.51)`,
`MetaSocialImage: 1_51/release-highlights.png`,
`Date: 2020-11-6`,
`DownloadVersion: 1.51.1`,
`---`,
`# October 2020 (version 1.51)`,
``,
`**Update 1.51.1**: The update addresses these [issues](https://github.com/microsoft/vscode/issues?q=is%3Aissue+milestone%3A%22October+2020+Recovery%22+is%3Aclosed+).`,
``,
`<!-- DOWNLOAD_LINKS_PLACEHOLDER -->`,
``,
`---`,
``,
`Welcome to the October 2020 release of Visual Studio Code. As announced in the [October iteration plan](https://github.com/microsoft/vscode/issues/108473), we focused on housekeeping GitHub issues and pull requests as documented in our issue grooming guide.`,
``,
`We also worked with our partners at GitHub on GitHub Codespaces, which ended up being more involved than originally anticipated. To that end, we'll continue working on housekeeping for part of the November iteration.`,
``,
`During this housekeeping milestone, we also addressed several feature requests and community [pull requests](#thank-you). Read on to learn about new features and settings.`,
``,
`## Workbench`,
``,
`### More prominent pinned tabs`,
``,
`${CURSOR}Pinned tabs will now always show their pin icon, even while inactive, to make them easier to identify. If an editor is both pinned and contains unsaved changes, the icon reflects both states.`,
``,
`![Inactive pinned tabs showing pin icons](images/1_51/pinned-tabs.png)`
)
);
assertNestedRangesEqual(ranges![0], [27, 0, 27, 201], [26, 0, 29, 70], [25, 0, 29, 70], [24, 0, 29, 70], [23, 0, 29, 70], [10, 0, 29, 70], [9, 0, 29, 70]);
});
});
function assertNestedLineNumbersEqual(range: vscode.SelectionRange, ...expectedRanges: [number, number][]) {
const lineage = getLineage(range);
assert.strictEqual(lineage.length, expectedRanges.length, `expected depth: ${expectedRanges.length}, but was ${lineage.length} ${getValues(lineage)}`);
for (let i = 0; i < lineage.length; i++) {
assertLineNumbersEqual(lineage[i], expectedRanges[i][0], expectedRanges[i][1], `parent at a depth of ${i}`);
}
}
function assertNestedRangesEqual(range: vscode.SelectionRange, ...expectedRanges: [number, number, number, number][]) {
const lineage = getLineage(range);
assert.strictEqual(lineage.length, expectedRanges.length, `expected depth: ${expectedRanges.length}, but was ${lineage.length} ${getValues(lineage)}`);
for (let i = 0; i < lineage.length; i++) {
assertLineNumbersEqual(lineage[i], expectedRanges[i][0], expectedRanges[i][2], `parent at a depth of ${i}`);
assert(lineage[i].range.start.character === expectedRanges[i][1], `parent at a depth of ${i} on start char`);
assert(lineage[i].range.end.character === expectedRanges[i][3], `parent at a depth of ${i} on end char`);
}
}
function getLineage(range: vscode.SelectionRange): vscode.SelectionRange[] {
const result: vscode.SelectionRange[] = [];
let currentRange: vscode.SelectionRange | undefined = range;
while (currentRange) {
result.push(currentRange);
currentRange = currentRange.parent;
}
return result;
}
function getValues(ranges: vscode.SelectionRange[]): string[] {
return ranges.map(range => {
return range.range.start.line + ' ' + range.range.start.character + ' ' + range.range.end.line + ' ' + range.range.end.character;
});
}
function assertLineNumbersEqual(selectionRange: vscode.SelectionRange, startLine: number, endLine: number, message: string) {
assert.strictEqual(selectionRange.range.start.line, startLine, `failed on start line ${message}`);
assert.strictEqual(selectionRange.range.end.line, endLine, `failed on end line ${message}`);
}
function getSelectionRangesForDocument(contents: string, pos?: vscode.Position[]): Promise<vscode.SelectionRange[] | undefined> {
const doc = new InMemoryDocument(testFileName, contents);
const workspace = new InMemoryMdWorkspace([doc]);
const engine = createNewMarkdownEngine();
const provider = new MdSmartSelect(engine, new MdTableOfContentsProvider(engine, workspace, nulLogger));
const positions = pos ? pos : getCursorPositions(contents, doc);
return provider.provideSelectionRanges(doc, positions, new vscode.CancellationTokenSource().token);
}