Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit cd0831b

Browse files
committed
fix(windows): closes #260
1 parent a4389fc commit cd0831b

8 files changed

Lines changed: 159 additions & 2 deletions

File tree

flow-libs/vscode-uri.js.flow

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* @flow */
2+
declare module 'vscode-uri' {
3+
import typeof { Uri } from 'vscode';
4+
declare export var URI: Uri;
5+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// flow-typed signature: 51a66a87f4c53bcd85ae2390149f70b0
2+
// flow-typed version: <<STUB>>/vscode-uri_v2.0.3/flow_v0.106.3
3+
4+
/**
5+
* This is an autogenerated libdef stub for:
6+
*
7+
* 'vscode-uri'
8+
*
9+
* Fill this stub out by replacing all the `any` types.
10+
*
11+
* Once filled out, we encourage you to share your work with the
12+
* community by sending a pull request to:
13+
* https://github.com/flowtype/flow-typed
14+
*/
15+
16+
declare module 'vscode-uri' {
17+
declare module.exports: any;
18+
}
19+
20+
/**
21+
* We include stubs for each file inside this npm package in case you need to
22+
* require those files directly. Feel free to delete any files that aren't
23+
* needed.
24+
*/
25+
declare module 'vscode-uri/lib/esm' {
26+
declare module.exports: any;
27+
}
28+
29+
declare module 'vscode-uri/lib/umd' {
30+
declare module.exports: any;
31+
}
32+
33+
// Filename aliases
34+
declare module 'vscode-uri/lib/esm/index' {
35+
declare module.exports: $Exports<'vscode-uri/lib/esm'>;
36+
}
37+
declare module 'vscode-uri/lib/esm/index.js' {
38+
declare module.exports: $Exports<'vscode-uri/lib/esm'>;
39+
}
40+
declare module 'vscode-uri/lib/umd/index' {
41+
declare module.exports: $Exports<'vscode-uri/lib/umd'>;
42+
}
43+
declare module 'vscode-uri/lib/umd/index.js' {
44+
declare module.exports: $Exports<'vscode-uri/lib/umd'>;
45+
}

lib/flowLSP/FlowLanguageClient/FlowLanguageClient.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as UUID from 'vscode-languageclient/lib/utils/uuid';
1111
import getFlowPath from '../utils/getFlowPath';
1212
import getFlowVersion from '../utils/getFlowVersion';
1313
import assertFlowSupportsLSP from '../utils/assertFlowSupportsLSP';
14+
import uriToString from '../utils/uriToString';
1415

1516
import Logger, { type LogLevel } from '../utils/Logger';
1617

@@ -184,7 +185,7 @@ export default class FlowLanguageClient {
184185
middleware: createMiddleware(flowconfigPath, flowVersion),
185186

186187
uriConverters: {
187-
code2Protocol: (uri) => uri.toString(true), // this disables URL-encoding for file URLs
188+
code2Protocol: uriToString,
188189
protocol2Code: (value) => vscode.Uri.parse(value),
189190
},
190191

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* @flow */
2+
import { URI } from 'vscode-uri';
3+
import uriToString from '../uriToString';
4+
5+
jest.mock('../isWindows', () => {
6+
return () => true;
7+
});
8+
9+
test('[windows] lower-case drive letter', () => {
10+
const uri = URI.file('c:/flow/test');
11+
expect(uriToString(uri)).toEqual('file:///C:/flow/test');
12+
});
13+
14+
test('[windows] upper-case drive letter', () => {
15+
const uri = URI.file('C:/flow/test');
16+
expect(uriToString(uri)).toEqual('file:///C:/flow/test');
17+
});
18+
19+
test('linux path', () => {
20+
const uri = URI.file('/flow/test');
21+
expect(uriToString(uri)).toEqual('file:///flow/test');
22+
});

lib/flowLSP/utils/isWindows.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* @flow */
2+
export default function isWindows(): boolean {
3+
return process.platform === 'win32';
4+
}

lib/flowLSP/utils/uriToString.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* @flow */
2+
import { type Uri } from 'vscode';
3+
import isWindows from './isWindows';
4+
5+
const CharCode = {
6+
Colon: 58,
7+
Slash: 47,
8+
A: 65,
9+
Z: 90,
10+
a: 97,
11+
z: 122,
12+
};
13+
14+
export default function uriToString(uri: Uri): string {
15+
return isWindows() ? uriToStringWindows(uri) : uri.toString(true);
16+
}
17+
18+
// NOTE: default `uri.toString` converts windows drive letters to lower-case
19+
// which causes issues with flow so below patching toString to return upper-case drive letters
20+
// see issue https://github.com/flowtype/flow-for-vscode/issues/260
21+
function uriToStringWindows(uri: Uri): string {
22+
const uriString = uri.toString(true);
23+
const windowsDriveLetter = getWindowsDriveLetter(uri);
24+
return windowsDriveLetter
25+
? makeWindowsDriveLetterUppercase(uriString, windowsDriveLetter)
26+
: uriString;
27+
}
28+
29+
function isAlphabet(charCode: number): boolean {
30+
return (
31+
(charCode >= CharCode.A && charCode <= CharCode.Z) ||
32+
(charCode >= CharCode.a && charCode <= CharCode.z)
33+
);
34+
}
35+
36+
function getWindowsDriveLetter(uri: Uri): ?string {
37+
const { path } = uri;
38+
39+
let driveLetter: ?string = null;
40+
// path of type '/c:/'
41+
if (
42+
path.length >= 3 &&
43+
path.charCodeAt(0) === CharCode.Slash &&
44+
path.charCodeAt(2) === CharCode.Colon
45+
) {
46+
const code = path.charCodeAt(1);
47+
if (
48+
(code >= CharCode.A && code <= CharCode.Z) ||
49+
(code >= CharCode.a && code <= CharCode.z)
50+
) {
51+
driveLetter = path.charAt(1);
52+
}
53+
// path of type 'c:/'
54+
} else if (path.length >= 2 && path.charCodeAt(1) === CharCode.Colon) {
55+
const code = path.charCodeAt(0);
56+
if (isAlphabet(code)) {
57+
driveLetter = path.charAt(0);
58+
}
59+
}
60+
61+
return driveLetter;
62+
}
63+
64+
function makeWindowsDriveLetterUppercase(
65+
uriString: string,
66+
windowsDriveLetter: string,
67+
): string {
68+
const lowerCaseDriveLetter = windowsDriveLetter.toLowerCase();
69+
const upperCaseDriveLetter = windowsDriveLetter.toUpperCase();
70+
return uriString.replace(
71+
`${lowerCaseDriveLetter}:`,
72+
`${upperCaseDriveLetter}:`,
73+
);
74+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@
246246
"rollup-plugin-commonjs": "10.0.2",
247247
"rollup-plugin-node-resolve": "5.2.0",
248248
"rollup-plugin-progress": "1.1.1",
249-
"rollup-plugin-terser": "5.1.1"
249+
"rollup-plugin-terser": "5.1.1",
250+
"vscode-uri": "2.0.3"
250251
},
251252
"icon": "flow-logo.png",
252253
"repository": {

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4826,6 +4826,11 @@ vscode-languageserver-types@3.14.0:
48264826
version "3.14.0"
48274827
resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.14.0.tgz#d3b5952246d30e5241592b6dde8280e03942e743"
48284828

4829+
vscode-uri@2.0.3:
4830+
version "2.0.3"
4831+
resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-2.0.3.tgz#25e5f37f552fbee3cec7e5f80cef8469cefc6543"
4832+
integrity sha512-4D3DI3F4uRy09WNtDGD93H9q034OHImxiIcSq664Hq1Y1AScehlP3qqZyTkX/RWxeu0MRMHGkrxYqm2qlDF/aw==
4833+
48294834
w3c-hr-time@^1.0.1:
48304835
version "1.0.1"
48314836
resolved "https://registry.yarnpkg.com/w3c-hr-time/-/w3c-hr-time-1.0.1.tgz#82ac2bff63d950ea9e3189a58a65625fedf19045"

0 commit comments

Comments
 (0)