Skip to content

Commit

Permalink
properly resolve ${file} variable; fixes microsoft#95423
Browse files Browse the repository at this point in the history
  • Loading branch information
weinand committed Jun 15, 2020
1 parent 89d0406 commit f9c6fa3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
38 changes: 23 additions & 15 deletions src/vs/workbench/api/common/extHostDebugService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface IExtHostDebugService extends ExtHostDebugServiceShape {
asDebugSourceUri(source: vscode.DebugProtocolSource, session?: vscode.DebugSession): vscode.Uri;
}

export class ExtHostDebugServiceBase implements IExtHostDebugService, ExtHostDebugServiceShape {
export abstract class ExtHostDebugServiceBase implements IExtHostDebugService, ExtHostDebugServiceShape {

readonly _serviceBrand: undefined;

Expand Down Expand Up @@ -373,9 +373,7 @@ export class ExtHostDebugServiceBase implements IExtHostDebugService, ExtHostDeb
return Promise.resolve(undefined);
}

protected createVariableResolver(folders: vscode.WorkspaceFolder[], editorService: ExtHostDocumentsAndEditors, configurationService: ExtHostConfigProvider): AbstractVariableResolverService {
return new ExtHostVariableResolverService(folders, editorService, configurationService);
}
protected abstract createVariableResolver(folders: vscode.WorkspaceFolder[], editorService: ExtHostDocumentsAndEditors, configurationService: ExtHostConfigProvider): AbstractVariableResolverService;

public async $substituteVariables(folderUri: UriComponents | undefined, config: IConfig): Promise<IConfig> {
if (!this._variableResolver) {
Expand Down Expand Up @@ -974,7 +972,7 @@ export class ExtHostDebugConsole implements vscode.DebugConsole {

export class ExtHostVariableResolverService extends AbstractVariableResolverService {

constructor(folders: vscode.WorkspaceFolder[], editorService: ExtHostDocumentsAndEditors, configurationService: ExtHostConfigProvider, env?: IProcessEnvironment) {
constructor(folders: vscode.WorkspaceFolder[], editorService: ExtHostDocumentsAndEditors | undefined, configurationService: ExtHostConfigProvider, env?: IProcessEnvironment) {
super({
getFolderUri: (folderName: string): URI | undefined => {
const found = folders.filter(f => f.name === folderName);
Expand All @@ -993,27 +991,33 @@ export class ExtHostVariableResolverService extends AbstractVariableResolverServ
return env ? env['VSCODE_EXEC_PATH'] : undefined;
},
getFilePath: (): string | undefined => {
const activeEditor = editorService.activeEditor();
if (activeEditor) {
return path.normalize(activeEditor.document.uri.fsPath);
if (editorService) {
const activeEditor = editorService.activeEditor();
if (activeEditor) {
return path.normalize(activeEditor.document.uri.fsPath);
}
}
return undefined;
},
getSelectedText: (): string | undefined => {
const activeEditor = editorService.activeEditor();
if (activeEditor && !activeEditor.selection.isEmpty) {
return activeEditor.document.getText(activeEditor.selection);
if (editorService) {
const activeEditor = editorService.activeEditor();
if (activeEditor && !activeEditor.selection.isEmpty) {
return activeEditor.document.getText(activeEditor.selection);
}
}
return undefined;
},
getLineNumber: (): string | undefined => {
const activeEditor = editorService.activeEditor();
if (activeEditor) {
return String(activeEditor.selection.end.line + 1);
if (editorService) {
const activeEditor = editorService.activeEditor();
if (activeEditor) {
return String(activeEditor.selection.end.line + 1);
}
}
return undefined;
}
}, env);
}, env, !editorService);
}
}

Expand Down Expand Up @@ -1104,4 +1108,8 @@ export class WorkerExtHostDebugService extends ExtHostDebugServiceBase {
) {
super(extHostRpcService, workspaceService, extensionService, editorsService, configurationService, commandService);
}

protected createVariableResolver(folders: vscode.WorkspaceFolder[], editorService: ExtHostDocumentsAndEditors, configurationService: ExtHostConfigProvider): AbstractVariableResolverService {
return new ExtHostVariableResolverService(folders, editorService, configurationService);
}
}
3 changes: 1 addition & 2 deletions src/vs/workbench/api/node/extHostDebugService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export class ExtHostDebugService extends ExtHostDebugServiceBase {
}

protected createVariableResolver(folders: vscode.WorkspaceFolder[], editorService: ExtHostDocumentsAndEditors, configurationService: ExtHostConfigProvider): AbstractVariableResolverService {
return new ExtHostVariableResolverService(folders, editorService, configurationService, process.env as env.IProcessEnvironment);
return new ExtHostVariableResolverService(folders, undefined, configurationService, process.env as env.IProcessEnvironment);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export interface IVariableResolveContext {
export class AbstractVariableResolverService implements IConfigurationResolverService {

static readonly VARIABLE_REGEXP = /\$\{(.*?)\}/g;
static readonly VARIABLE_REGEXP_SINGLE = /\$\{(.*?)\}/;

declare readonly _serviceBrand: undefined;

Expand All @@ -37,7 +36,7 @@ export class AbstractVariableResolverService implements IConfigurationResolverSe
protected _contributedVariables: Map<string, () => Promise<string | undefined>> = new Map();


constructor(_context: IVariableResolveContext, _envVariables?: IProcessEnvironment) {
constructor(_context: IVariableResolveContext, _envVariables?: IProcessEnvironment, private _ignoreEditorVariables = false) {
this._context = _context;
if (_envVariables) {
if (isWindows) {
Expand Down Expand Up @@ -232,45 +231,72 @@ export class AbstractVariableResolverService implements IConfigurationResolverSe
return paths.basename(getFolderUri().fsPath);

case 'lineNumber':
if (this._ignoreEditorVariables) {
return match;
}
const lineNumber = this._context.getLineNumber();
if (lineNumber) {
return lineNumber;
}
throw new Error(localize('canNotResolveLineNumber', "'{0}' can not be resolved. Make sure to have a line selected in the active editor.", match));

case 'selectedText':
if (this._ignoreEditorVariables) {
return match;
}
const selectedText = this._context.getSelectedText();
if (selectedText) {
return selectedText;
}
throw new Error(localize('canNotResolveSelectedText', "'{0}' can not be resolved. Make sure to have some text selected in the active editor.", match));

case 'file':
if (this._ignoreEditorVariables) {
return match;
}
return getFilePath();

case 'relativeFile':
if (this._ignoreEditorVariables) {
return match;
}
if (folderUri || argument) {
return paths.normalize(paths.relative(getFolderUri().fsPath, getFilePath()));
}
return getFilePath();

case 'relativeFileDirname':
if (this._ignoreEditorVariables) {
return match;
}
const dirname = paths.dirname(getFilePath());
if (folderUri || argument) {
return paths.normalize(paths.relative(getFolderUri().fsPath, dirname));
}
return dirname;

case 'fileDirname':
if (this._ignoreEditorVariables) {
return match;
}
return paths.dirname(getFilePath());

case 'fileExtname':
if (this._ignoreEditorVariables) {
return match;
}
return paths.extname(getFilePath());

case 'fileBasename':
if (this._ignoreEditorVariables) {
return match;
}
return paths.basename(getFilePath());

case 'fileBasenameNoExtension':
if (this._ignoreEditorVariables) {
return match;
}
const basename = paths.basename(getFilePath());
return (basename.slice(0, basename.length - paths.extname(basename).length));

Expand Down

0 comments on commit f9c6fa3

Please sign in to comment.