Skip to content

Commit

Permalink
Update to vscode 1.62.3
Browse files Browse the repository at this point in the history
  • Loading branch information
GehDoc committed May 13, 2022
1 parent 9915333 commit d46f680
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 315 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- [CI] Replace Travis by Github action
- [DOC] Update badges
- Update to vscode 1.62.3

### Added
- [CI] Run test suite on Windows also
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@
},
"devDependencies": {
"@types/lodash.throttle": "^4.1.3",
"@types/mocha": "^8.2.0",
"@types/mocha": "^9.1.1",
"@types/node": "16.x",
"@types/vscode": "^1.61.0",
"@types/vscode-webview": "^1.57.0",
Expand All @@ -373,12 +373,12 @@
"gulp-typescript": "^5.0.1",
"lodash.throttle": "^4.1.1",
"merge-options": "^1.0.1",
"mocha": "^8.2.1",
"mocha": "^9.2.2",
"mocha-junit-reporter": "^2.0.0",
"mocha-multi-reporters": "^1.5.1",
"path-browserify": "^1.0.1",
"ts-loader": "^9.2.3",
"typescript": "^4.5.0-dev.20210927",
"typescript": "^4.8.0-dev.20220511",
"util": "^0.12.4",
"vsce": "^1.100.1",
"vscode-nls-dev": "^3.3.2",
Expand Down
131 changes: 3 additions & 128 deletions src/commands/openDocumentLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import * as vscode from 'vscode';
import { Command } from '../commandManager';
import { TextileEngine } from '../textileEngine';
import { TableOfContentsProvider } from '../tableOfContentsProvider';
import { isTextileFile } from '../util/file';
import { extname } from '../util/path';
import { openDocumentLink } from '../util/openDocumentLink';


type UriComponents = {
Expand All @@ -25,11 +23,6 @@ export interface OpenDocumentLinkArgs {
readonly fromResource: UriComponents;
}

enum OpenTextileLinks {
beside = 'beside',
currentGroup = 'currentGroup',
}

export class OpenDocumentLinkCommand implements Command {
private static readonly id = '_textile.openDocumentLink';
public readonly id = OpenDocumentLinkCommand.id;
Expand Down Expand Up @@ -60,95 +53,10 @@ export class OpenDocumentLinkCommand implements Command {
) { }

public async execute(args: OpenDocumentLinkArgs) {
return OpenDocumentLinkCommand.execute(this.engine, args);
}

public static async execute(engine: TextileEngine, args: OpenDocumentLinkArgs): Promise<void> {
const fromResource = vscode.Uri.parse('').with(args.fromResource);

const targetResource = reviveUri(args.parts);

const column = this.getViewColumn(fromResource);

const didOpen = await this.tryOpen(engine, targetResource, args, column);
if (didOpen) {
return;
}

if (extname(targetResource.path) === '') {
await this.tryOpen(engine, targetResource.with({ path: targetResource.path + '.textile' }), args, column);
return;
}
}

private static async tryOpen(engine: TextileEngine, resource: vscode.Uri, args: OpenDocumentLinkArgs, column: vscode.ViewColumn): Promise<boolean> {
const tryUpdateForActiveFile = async (): Promise<boolean> => {
if (vscode.window.activeTextEditor && isTextileFile(vscode.window.activeTextEditor.document)) {
if (vscode.window.activeTextEditor.document.uri.fsPath === resource.fsPath) {
await this.tryRevealLine(engine, vscode.window.activeTextEditor, args.fragment);
return true;
}
}
return false;
};

if (await tryUpdateForActiveFile()) {
return true;
}

let stat: vscode.FileStat;
try {
stat = await vscode.workspace.fs.stat(resource);
if (stat.type === vscode.FileType.Directory) {
await vscode.commands.executeCommand('revealInExplorer', resource);
return true;
}
} catch {
// noop
// If resource doesn't exist, execute `vscode.open` either way so an error
// notification is shown to the user with a create file action #113475
}

try {
await vscode.commands.executeCommand('vscode.open', resource, column);
} catch {
return false;
}

return tryUpdateForActiveFile();
}

private static getViewColumn(resource: vscode.Uri): vscode.ViewColumn {
const config = vscode.workspace.getConfiguration('textile', resource);
const openLinks = config.get<OpenTextileLinks>('links.openLocation', OpenTextileLinks.currentGroup);
switch (openLinks) {
case OpenTextileLinks.beside:
return vscode.ViewColumn.Beside;
case OpenTextileLinks.currentGroup:
default:
return vscode.ViewColumn.Active;
}
}

private static async tryRevealLine(engine: TextileEngine, editor: vscode.TextEditor, fragment?: string) {
if (fragment) {
const toc = new TableOfContentsProvider(engine, editor.document);
const entry = await toc.lookup(fragment);
if (entry) {
const lineStart = new vscode.Range(entry.line, 0, entry.line, 0);
editor.selection = new vscode.Selection(lineStart.start, lineStart.end);
return editor.revealRange(lineStart, vscode.TextEditorRevealType.AtTop);
}
const lineNumberFragment = fragment.match(/^L(\d+)$/i);
if (lineNumberFragment) {
const line = +lineNumberFragment[1] - 1;
if (!isNaN(line)) {
const lineStart = new vscode.Range(line, 0, line, 0);
editor.selection = new vscode.Selection(lineStart.start, lineStart.end);
return editor.revealRange(lineStart, vscode.TextEditorRevealType.AtTop);
}
}
}
const targetResource = reviveUri(args.parts).with({ fragment: args.fragment });
return openDocumentLink(this.engine, targetResource, fromResource);
}
}

Expand All @@ -158,36 +66,3 @@ function reviveUri(parts: any) {
}
return vscode.Uri.parse('').with(parts);
}

export async function resolveLinkToTextileFile(path: string): Promise<vscode.Uri | undefined> {
try {
const standardLink = await tryResolveLinkToTextileFile(path);
if (standardLink) {
return standardLink;
}
} catch {
// Noop
}

// If no extension, try with `.textile` extension
if (extname(path) === '') {
return tryResolveLinkToTextileFile(path + '.textile');
}

return undefined;
}

async function tryResolveLinkToTextileFile(path: string): Promise<vscode.Uri | undefined> {
const resource = vscode.Uri.file(path);

let document: vscode.TextDocument;
try {
document = await vscode.workspace.openTextDocument(resource);
} catch {
return undefined;
}
if (isTextileFile(document)) {
return document.uri;
}
return undefined;
}
12 changes: 11 additions & 1 deletion src/features/documentLinkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ function parseLink(
document: vscode.TextDocument,
link: string,
): { uri: vscode.Uri, tooltip?: string } | undefined {
const externalSchemeUri = getUriForLinkWithKnownExternalScheme(link);
const cleanLink = stripAngleBrackets(link);
const externalSchemeUri = getUriForLinkWithKnownExternalScheme(cleanLink);
if (externalSchemeUri) {
// Normalize VS Code links to target currently running version
if (isOfScheme(Schemes.vscode, link) || isOfScheme(Schemes['vscode-insiders'], link)) {
Expand Down Expand Up @@ -89,6 +90,15 @@ function extractDocumentLink(
}
}

/* Used to strip brackets from the textile link
<http://example.com> will be transformed to
http://example.com
*/
export function stripAngleBrackets(link: string) {
const bracketMatcher = /^<(.*)>$/;
return link.replace(bracketMatcher, '$1');
}

// -- Begin: Added for textile
function getDocumentLink(
document: vscode.TextDocument,
Expand Down
14 changes: 11 additions & 3 deletions src/features/foldingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { TableOfContentsProvider } from '../tableOfContentsProvider';

const rangeLimit = 5000;

interface TextileTokenWithMap extends Token {
map: [number, number];
}

export default class TextileFoldingProvider implements vscode.FoldingRangeProvider {

constructor(
Expand Down Expand Up @@ -139,16 +143,20 @@ export default class TextileFoldingProvider implements vscode.FoldingRangeProvid
const isStartRegion = (t: string) => /^\s*#?region\b.*/.test(t);
const isEndRegion = (t: string) => /^\s*#?endregion\b.*/.test(t);

const isRegionMarker = (token: Token) =>
typeof(token[0]) === 'string' && token[0] === '!' && typeof(token[1]) === 'object' && typeof(token[2]) === 'string' && (isStartRegion(token[2]) || isEndRegion(token[2]));
const isRegionMarker = (token: Token): token is TextileTokenWithMap =>
!!token.map && typeof(token[0]) === 'string' && token[0] === '!' && typeof(token[1]) === 'object' && typeof(token[2]) === 'string' && (isStartRegion(token[2]) || isEndRegion(token[2]));

const getLineNumber = (token: Token) =>
typeof(token[0]) === 'string' && typeof(token[1]) === 'object' && typeof(token[1]['data-line']) !== 'undefined' ? +token[1]['data-line'] : undefined;

const getEndLineNumber = (token: Token) =>
typeof(token[0]) === 'string' && typeof(token[1]) === 'object' && typeof(token[1]['data-line-end']) !== 'undefined' ? +token[1]['data-line-end'] : undefined;

const isFoldableToken = (token: Token): boolean => {
const isFoldableToken = (token: Token): token is TextileTokenWithMap => {
if (!token.map) {
return false;
}

switch (token[0]) {
case 'li':
case 'pre':
Expand Down
20 changes: 5 additions & 15 deletions src/features/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import { OpenDocumentLinkCommand, resolveLinkToTextileFile } from '../commands/openDocumentLink';
import { Logger } from '../logger';
import { TextileEngine } from '../textileEngine';
import { TextileContributionProvider } from '../textileExtensions';
import { Disposable } from '../util/dispose';
import { isTextileFile } from '../util/file';
import { openDocumentLink, resolveDocumentLink, resolveLinkToTextileFile } from '../util/openDocumentLink';
import * as path from '../util/path';
import { WebviewResourceProvider } from '../util/resources';
import { getVisibleLine, LastScrollLocation, TopmostLineMonitor } from '../util/topmostLineMonitor';
Expand Down Expand Up @@ -429,29 +429,19 @@ class TextilePreview extends Disposable implements WebviewResourceProvider {


private async onDidClickPreviewLink(href: string) {
let [hrefPath, fragment] = href.split('#').map(c => decodeURIComponent(c));

if (hrefPath[0] !== '/') {
// We perviously already resolve absolute paths.
// Now make sure we handle relative file paths
const dirnameUri = vscode.Uri.parse(path.dirname(this.resource.path));
hrefPath = vscode.Uri.joinPath(dirnameUri, hrefPath).path;
} else {
// Handle any normalized file paths
hrefPath = vscode.Uri.parse(hrefPath.replace('/file', '')).path;
}
const targetResource = resolveDocumentLink(href, this.resource);

const config = vscode.workspace.getConfiguration('textile', this.resource);
const openLinks = config.get<string>('preview.openTextileLinks', 'inPreview');
if (openLinks === 'inPreview') {
const textileLink = await resolveLinkToTextileFile(hrefPath);
const textileLink = await resolveLinkToTextileFile(targetResource);
if (textileLink) {
this.delegate.openPreviewLinkToTextileFile(textileLink, fragment);
this.delegate.openPreviewLinkToTextileFile(textileLink, targetResource.fragment);
return;
}
}

OpenDocumentLinkCommand.execute(this.engine, { parts: { path: hrefPath }, fragment, fromResource: this.resource.toJSON() });
return openDocumentLink(this.engine, targetResource, this.resource);
}

//#region WebviewResourceProvider
Expand Down
10 changes: 10 additions & 0 deletions src/test/documentLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ suite('Textile Document links', () => {
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 1);
});

test('Should navigate to line number within non-md file', async () => {
await withFileContents(testFileA, '"b":sub/foo.txt#L3');

const [link] = await getLinksForFile(testFileA);
await executeLink(link);

assertActiveDocumentUri(workspaceFile('sub', 'foo.txt'));
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 2);
});

test('Should navigate to fragment within current file', async () => {
await withFileContents(testFileA, joinLines(
'"current":a#header',
Expand Down
2 changes: 1 addition & 1 deletion src/test/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ suite('textile.engine', () => {
assert.deepStrictEqual((await engine.render(input)), {
html: '<p data-line="0" class="code-line">'
+ '<img src="img.png" data-line="0" class="code-line loading" alt="" id="image-hash--754511435" data-src="img.png" /> '
+ '<a href="no-img.png">a</a> '
+ '<a href="no-img.png" data-href="no-img.png">a</a> '
+ '<img src="http://example.org/img.png" data-line="0" class="code-line loading" alt="" id="image-hash--1903814170" data-src="http://example.org/img.png" /> '
+ '<img src="img.png" data-line="0" class="code-line loading" alt="" id="image-hash--754511435" data-src="img.png" /> '
+ '<img src="./img2.png" data-line="0" class="code-line loading" alt="" id="image-hash-265238964" data-src="./img2.png" />'
Expand Down
Loading

0 comments on commit d46f680

Please sign in to comment.