Skip to content

Commit

Permalink
Fix file path mapping functions to use the correct schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
PranayAgarwal committed May 15, 2018
1 parent 27d7def commit 3c623ac
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 49 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

See the full list of recent releases and features added on the [Github releases page](https://github.com/PranayAgarwal/vscode-hack/releases).

## v0.8.1 - 2018-05-14
- Updated Hack language syntax to the latest version
- Removed some unnecessary PHP snippets
- Fixed file path mapping in typechecker requests & responses to use the correct scheme (thanks [@fredemmott](https://github.com/fredemmott) for the thorough investigation)

## v0.8.0 - 2018-05-10
- **HHVM Debugger (Alpha version)** — Launch scripts or attach to an HHVM server straight from VS Code. See the [debugger doc](https://github.com/PranayAgarwal/vscode-hack/blob/master/docs/debugging.md) for details on setup and usage. _This is a very early release. Please file any bugs at the Issues page._
- Hack coverage check works again. A new icon in the editor status bar shows % coverage for the file and can be clicked to highlight uncovered areas. (Can be disabled by setting `"hack.enableCoverageCheck": false`)
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ It is published in the Visual Studio Marketplace [here](https://marketplace.visu

## Latest releases

## v0.8.1
- Updated Hack language syntax to the latest version
- Removed some unnecessary PHP snippets
- Fixed file path mapping in typechecker requests & responses to use the correct scheme (thanks [@fredemmott](https://github.com/fredemmott) for the thorough investigation)

## v0.8.0
- **HHVM Debugger (Alpha version)** — Launch scripts or attach to an HHVM server straight from VS Code. See the [debugger doc](https://github.com/PranayAgarwal/vscode-hack/blob/master/docs/debugging.md) for details on setup and usage. _This is a very early release. Please file any bugs at the Issues page._
- Hack coverage check works again. A new icon in the editor status bar shows % coverage for the file and can be clicked to highlight uncovered areas. (Can be disabled by setting `"hack.enableCoverageCheck": false`)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vscode-hack",
"version": "0.8.0",
"version": "0.8.1",
"publisher": "pranayagarwal",
"engines": {
"vscode": "^1.18.0"
Expand Down
10 changes: 9 additions & 1 deletion src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ export const clientPath: string = hackConfig.get('clientPath') || 'hh_client';
export const hhClientArgs: string[] = clientPath.split(' ');
export const hhClientCommand: string = String(hhClientArgs.shift());

export const workspace: string = hackConfig.get('workspaceRootPath') || vscode.workspace.rootPath || '';
export let mapWorkspace: boolean = false;
export let workspace: string;
const workspaceRootPath: string | undefined = hackConfig.get('workspaceRootPath');
if (workspaceRootPath) {
mapWorkspace = true;
workspace = workspaceRootPath;
} else if (vscode.workspace.workspaceFolders) {
workspace = vscode.workspace.workspaceFolders[0].uri.fsPath;
}

let enableCoverageCheckConfig: boolean | undefined = hackConfig.get('enableConverageCheck');
if (enableCoverageCheckConfig === undefined) {
Expand Down
59 changes: 27 additions & 32 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,38 @@ import * as vscode from 'vscode';
import * as config from './Config';

/**
* If a workspace mapping is specified in vscode settings, convert a local workspace file
* path's root to the configured alternate path.
* @param fileName The file path to convert
* Converts a local workspace URI to a file path string (with or without scheme) to pass to
* the typechecker. Path is mapped to an alternate workspace root if configured.
* @param file The file URI to convert
* @param includeScheme Whether to include the file:// scheme in the response or not
*/
export const mapFromWorkspacePath = (fileName: string) => {
if (config.workspace && vscode.workspace.rootPath) {
return fileName.replace(vscode.workspace.rootPath, config.workspace);
export const mapFromWorkspaceUri = (file: vscode.Uri, includeScheme: boolean = true): string => {
if (!config.mapWorkspace && includeScheme) {
return file.toString();
}
return fileName;
};

/**
* If a workspace mapping is specified in vscode settings, convert a mapped file path's
* root back to the local workspace.
* @param fileName The file path to convert
*/
export const mapToWorkspacePath = (fileName: string) => {
if (config.workspace && vscode.workspace.rootPath) {
return fileName.replace(config.workspace, vscode.workspace.rootPath);
let filePath = file.fsPath;
if (config.mapWorkspace && vscode.workspace.workspaceFolders) {
filePath = filePath.replace(vscode.workspace.workspaceFolders[0].uri.fsPath, config.workspace);
}
return fileName;
};

/**
* Converts a local workspace URI to a file path string, mapping path to an
* alternate workspace root if configured.
* @param value The file URI to convert
*/
export const mapFromWorkspaceUri = (value: vscode.Uri) => {
return mapFromWorkspacePath(value.fsPath);
if (includeScheme) {
return vscode.Uri.file(filePath).toString();
}
return filePath;
};

/**
* Converts a file path string to a local workspace URI, mapping from an
* alternate workspace root if configured.
* @param value The file path to convert
* Converts a file path string received from the typechecker to a local workspace URI.
* Path is mapped from an alternate workspace root if configured.
* @param file The file path to convert
*/
export const mapToWorkspaceUri = (value: string) => {
return vscode.Uri.parse(mapToWorkspacePath(value));
export const mapToWorkspaceUri = (file: string): vscode.Uri => {
let filePath = file;
if (config.mapWorkspace && vscode.workspace.workspaceFolders) {
filePath = filePath.replace(config.workspace, vscode.workspace.workspaceFolders[0].uri.fsPath);
}
if (filePath.startsWith('file://')) {
return vscode.Uri.parse(filePath);
} else {
return vscode.Uri.file(filePath);
}
};
14 changes: 7 additions & 7 deletions src/coveragechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import * as hh_client from './proxy';
import * as utils from './Utils';

type UnfilteredTypeCoverageRegion = {
regionType: string,
line: number,
start: number,
end: number
regionType: string;
line: number;
start: number;
end: number;
};

export class HackCoverageChecker {
Expand All @@ -36,8 +36,8 @@ export class HackCoverageChecker {
* until hhvm returns a better response for coverage runs.
*
*/
private static convertTypedRegionsToCoverageResult(regions: { color: string, text: string }[])
: { percentage: number, uncoveredRegions: UnfilteredTypeCoverageRegion[] } {
private static convertTypedRegionsToCoverageResult(regions: { color: string; text: string }[])
: { percentage: number; uncoveredRegions: UnfilteredTypeCoverageRegion[] } {
const startColumn = 1;
let line = 1;
let column = startColumn;
Expand Down Expand Up @@ -135,7 +135,7 @@ export class HackCoverageChecker {
this.coverageStatus.hide();
return;
}
const colorResult = await hh_client.color(utils.mapFromWorkspacePath(document.fileName));
const colorResult = await hh_client.color(utils.mapFromWorkspaceUri(document.uri, false));
if (!colorResult) {
this.coverageStatus.hide();
return;
Expand Down
9 changes: 4 additions & 5 deletions src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const pushSymbols = (outline: OutlineResponse[], symbols: vscode.SymbolInformati
name += '()';
break;
default:
break;
}
name = indent + name;
const range = getRange(element.span.line_start, element.span.line_end, element.span.char_start, element.span.char_end);
Expand All @@ -74,7 +73,7 @@ export class HackHoverProvider implements vscode.HoverProvider {
const startPosition = wordPosition.start;
const line: number = startPosition.line + 1;
const character: number = startPosition.character + 1;
return hh_client.typeAtPos(utils.mapFromWorkspacePath(document.fileName), line, character).then(hoverType => {
return hh_client.typeAtPos(utils.mapFromWorkspaceUri(document.uri, false), line, character).then(hoverType => {
if (!hoverType) {
return;
}
Expand Down Expand Up @@ -108,7 +107,7 @@ export class HackWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvid
desc = desc.slice(0, element.desc.indexOf(' in '));
}
const kind = getSymbolKind(desc);
const uri: vscode.Uri = vscode.Uri.file(utils.mapToWorkspacePath(element.filename));
const uri: vscode.Uri = utils.mapToWorkspaceUri(element.filename);
const container = element.scope || (element.name.includes('\\') ? element.name.slice(0, element.name.lastIndexOf('\\')) : undefined);
const range = getRange(element.line, element.line, element.char_start, element.char_end);
symbols.push(new vscode.SymbolInformation(name, kind, range, uri, container));
Expand Down Expand Up @@ -190,7 +189,7 @@ export class HackReferenceProvider implements vscode.ReferenceProvider {
const locations: vscode.Location[] = [];
foundRefs.forEach(ref => {
const location = new vscode.Location(
vscode.Uri.file(utils.mapToWorkspacePath(ref.filename)),
utils.mapToWorkspaceUri(ref.filename),
new vscode.Range(
new vscode.Position(ref.line - 1, ref.char_start - 1),
new vscode.Position(ref.line - 1, ref.char_end)));
Expand Down Expand Up @@ -218,7 +217,7 @@ export class HackDefinitionProvider implements vscode.DefinitionProvider {
foundDefinition.forEach(element => {
if (element.definition_pos) {
const location: vscode.Location = new vscode.Location(
vscode.Uri.file(utils.mapToWorkspacePath(element.definition_pos.filename) || document.fileName),
element.definition_pos.filename ? utils.mapToWorkspaceUri(element.definition_pos.filename) : document.uri,
new vscode.Range(
new vscode.Position(element.definition_pos.line - 1, element.definition_pos.char_start - 1),
new vscode.Position(element.definition_pos.line - 1, element.definition_pos.char_end)));
Expand Down
6 changes: 3 additions & 3 deletions src/typechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class HackTypeChecker {
return;
}

const diagnosticMap: Map<string, vscode.Diagnostic[]> = new Map();
const diagnosticMap: Map<vscode.Uri, vscode.Diagnostic[]> = new Map();
typecheckResult.errors.forEach(error => {
let fullMessage = '';
let code: number = 0;
Expand All @@ -39,7 +39,7 @@ export class HackTypeChecker {
vscode.DiagnosticSeverity.Error);
diagnostic.code = code;
diagnostic.source = 'Hack';
const file = utils.mapToWorkspacePath(error.message[0].path);
const file = utils.mapToWorkspaceUri(error.message[0].path);
const cachedFileDiagnostics = diagnosticMap.get(file);
if (cachedFileDiagnostics) {
cachedFileDiagnostics.push(diagnostic);
Expand All @@ -48,7 +48,7 @@ export class HackTypeChecker {
}
});
diagnosticMap.forEach((diags, file) => {
this.hhvmTypeDiag.set(vscode.Uri.file(file), diags);
this.hhvmTypeDiag.set(file, diags);
});
}
}

0 comments on commit 3c623ac

Please sign in to comment.