-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20582 from Microsoft/tyriar/7321
Terminal links
- Loading branch information
Showing
6 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/vs/workbench/parts/terminal/electron-browser/terminalLinkHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as path from 'path'; | ||
import * as pfs from 'vs/base/node/pfs'; | ||
import Uri from 'vs/base/common/uri'; | ||
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService'; | ||
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace'; | ||
import { Platform } from 'vs/base/common/platform'; | ||
import { TPromise } from 'vs/base/common/winjs.base'; | ||
|
||
const pathPrefix = '(\\.\\.?|\\~)'; | ||
const pathSeparatorClause = '\\/'; | ||
const excludedPathCharactersClause = '[^\\0\\s!$`&*()+\'":;]'; // '":; are allowed in paths but they are often separators so ignore them | ||
const escapedExcludedPathCharactersClause = '(\\\\s|\\\\!|\\\\$|\\\\`|\\\\&|\\\\*|(|)|\\+)'; | ||
/** A regex that matches paths in the form /path, ~/path, ./path, ../path */ | ||
const UNIX_LIKE_LOCAL_LINK_REGEX = new RegExp('(' + pathPrefix + '?(' + pathSeparatorClause + '(' + excludedPathCharactersClause + '|' + escapedExcludedPathCharactersClause + ')+)+)'); | ||
|
||
const winPathPrefix = '([a-zA-Z]:|\\.\\.?|\\~)'; | ||
const winPathSeparatorClause = '(\\\\|\\/)'; | ||
const winExcludedPathCharactersClause = '[^\\0<>\\?\\|\\/\\s!$`&*()+\'":;]'; | ||
/** A regex that matches paths in the form c:\path, ~\path, .\path */ | ||
const WINDOWS_LOCAL_LINK_REGEX = new RegExp('(' + winPathPrefix + '?(' + winPathSeparatorClause + '(' + winExcludedPathCharactersClause + ')+)+)'); | ||
|
||
export class TerminalLinkHandler { | ||
constructor( | ||
private _platform: Platform, | ||
@IWorkbenchEditorService private _editorService: IWorkbenchEditorService, | ||
@IWorkspaceContextService private _contextService: IWorkspaceContextService | ||
) { | ||
} | ||
|
||
public get localLinkRegex(): RegExp { | ||
if (this._platform === Platform.Windows) { | ||
return WINDOWS_LOCAL_LINK_REGEX; | ||
} | ||
return UNIX_LIKE_LOCAL_LINK_REGEX; | ||
} | ||
|
||
public handleLocalLink(link: string): TPromise<void> { | ||
if (this._platform === Platform.Windows) { | ||
return this._handleWindowsLocalLink(link); | ||
} | ||
return this._handleUnixLikeLocalLink(link); | ||
} | ||
|
||
private _handleUnixLikeLocalLink(link: string): TPromise<void> { | ||
// Resolve ~ -> $HOME | ||
if (link.charAt(0) === '~') { | ||
if (!process.env.HOME) { | ||
return TPromise.as(void 0); | ||
} | ||
link = process.env.HOME + link.substring(1); | ||
} | ||
return this._handleCommonLocalLink(link); | ||
} | ||
|
||
private _handleWindowsLocalLink(link: string): TPromise<void> { | ||
// Resolve ~ -> %HOMEDRIVE%\%HOMEPATH% | ||
if (link.charAt(0) === '~') { | ||
if (!process.env.HOMEDRIVE || !process.env.HOMEPATH) { | ||
return TPromise.as(void 0); | ||
} | ||
link = `${process.env.HOMEDRIVE}\\${process.env.HOMEPATH + link.substring(1)}`; | ||
} | ||
return this._handleCommonLocalLink(link); | ||
} | ||
|
||
private _handleCommonLocalLink(link: string): TPromise<void> { | ||
// Resolve workspace path . / .. -> <path>/. / <path/.. | ||
if (link.charAt(0) === '.') { | ||
if (!this._contextService.hasWorkspace) { | ||
// Abort if no workspace is open | ||
return TPromise.as(void 0); | ||
} | ||
link = path.join(this._contextService.getWorkspace().resource.fsPath, link); | ||
} | ||
|
||
// Clean up the path | ||
const resource = Uri.file(path.normalize(path.resolve(link))); | ||
|
||
// Open an editor if the path exists | ||
return pfs.fileExists(link).then(isFile => { | ||
if (!isFile) { | ||
return void 0; | ||
} | ||
return this._editorService.openEditor({ resource }).then(() => void 0); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/vs/workbench/parts/terminal/test/electron-browser/terminalLinkHandler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
import * as assert from 'assert'; | ||
import { Platform } from 'vs/base/common/platform'; | ||
import { TerminalLinkHandler } from 'vs/workbench/parts/terminal/electron-browser/terminalLinkHandler'; | ||
|
||
suite('Workbench - TerminalLinkHandler', () => { | ||
suite('localLinkRegex', () => { | ||
test('Windows', () => { | ||
const regex = new TerminalLinkHandler(Platform.Windows, null, null).localLinkRegex; | ||
function testLink(link: string) { | ||
assert.equal(` ${link} `.match(regex)[1], link); | ||
assert.equal(`:${link}:`.match(regex)[1], link); | ||
assert.equal(`;${link};`.match(regex)[1], link); | ||
assert.equal(`(${link})`.match(regex)[1], link); | ||
} | ||
testLink('c:\\foo'); | ||
testLink('c:/foo'); | ||
testLink('.\\foo'); | ||
testLink('./foo'); | ||
testLink('..\\foo'); | ||
testLink('../foo'); | ||
testLink('~\\foo'); | ||
testLink('~/foo'); | ||
testLink('c:/a/long/path'); | ||
testLink('c:\\a\\long\\path'); | ||
testLink('c:\\mixed/slash\\path'); | ||
}); | ||
|
||
test('Linux', () => { | ||
const regex = new TerminalLinkHandler(Platform.Linux, null, null).localLinkRegex; | ||
function testLink(link: string) { | ||
assert.equal(` ${link} `.match(regex)[1], link); | ||
assert.equal(`:${link}:`.match(regex)[1], link); | ||
assert.equal(`;${link};`.match(regex)[1], link); | ||
assert.equal(`(${link})`.match(regex)[1], link); | ||
} | ||
testLink('/foo'); | ||
testLink('~/foo'); | ||
testLink('./foo'); | ||
testLink('../foo'); | ||
testLink('/a/long/path'); | ||
}); | ||
}); | ||
}); |