Skip to content

Commit

Permalink
Misc file tweaks (#399)
Browse files Browse the repository at this point in the history
* Rename pathAbsolute to srcPath

* Convert pkgPath to actual pkgPath

* Simplify Program removeFile and addReplaceFile

* Add normalizePath param to `scope.GetFile`

* Minor tweaks

* Remove unnecessary standardizePath calls in xmlFile.spec.ts

* Split using regex for better stability

* fixed merge-leftover `pathAbsolute` vars

* Address PR concerns: support any path protocol

* Fix `startsWithProtocol` regex issue
  • Loading branch information
TwitchBronBron authored May 6, 2021
1 parent 24e8ac9 commit 0c0b97e
Show file tree
Hide file tree
Showing 31 changed files with 584 additions and 490 deletions.
2 changes: 1 addition & 1 deletion benchmarks/targets/parse-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = async (suite, name, brighterscript, projectPath, options) => {
suite.add(name, (deferred) => {
const wait = [];
for (const x of xmlFiles) {
const xmlFile = new XmlFile(x.pathAbsolute, x.pkgPath, builder.program);
const xmlFile = new XmlFile(x.srcPath || x.pathAbsolute, x.pkgPath, builder.program);
//handle async and sync parsing
const prom = xmlFile.parse(x.fileContents);
if (prom) {
Expand Down
2 changes: 1 addition & 1 deletion docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ interface FileObj {
}

interface SourceObj {
pathAbsolute: string;
srcPath: string;
source: string;
}

Expand Down
4 changes: 2 additions & 2 deletions src/DiagnosticCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('DiagnosticCollection', () => {
function removeDiagnostic(filePath: string, message: string) {
for (let i = 0; i < diagnostics.length; i++) {
const diagnostic = diagnostics[i];
if (diagnostic.file.pathAbsolute === filePath && diagnostic.message === message) {
if (diagnostic.file.srcPath === filePath && diagnostic.message === message) {
diagnostics.splice(i, 1);
return;
}
Expand All @@ -99,7 +99,7 @@ describe('DiagnosticCollection', () => {
for (const message of messages) {
diagnostics.push({
file: {
pathAbsolute: filePath
srcPath: filePath
} as BscFile,
range: util.createRange(0, 0, 0, 0),
//the code doesn't matter as long as the messages are different, so just enforce unique messages for this test files
Expand Down
4 changes: 2 additions & 2 deletions src/DiagnosticCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ export class DiagnosticCollection {
const keys = {};
//build the full current set of diagnostics by file
for (let diagnostic of diagnostics) {
const filePath = diagnostic.file.pathAbsolute;
const filePath = diagnostic.file.srcPath;
//ensure the file entry exists
if (!result[filePath]) {
result[filePath] = [];
}
const diagnosticMap = result[filePath];

diagnostic.key =
diagnostic.file.pathAbsolute.toLowerCase() + '-' +
diagnostic.file.srcPath.toLowerCase() + '-' +
diagnostic.code + '-' +
diagnostic.range.start.line + '-' +
diagnostic.range.start.character + '-' +
Expand Down
4 changes: 2 additions & 2 deletions src/DiagnosticFilterer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ describe('DiagnosticFilterer', () => {

});

function getDiagnostic(code: number | string, pathAbsolute: string) {
function getDiagnostic(code: number | string, srcPath: string) {
return {
file: {
pathAbsolute: s`${pathAbsolute}`
srcPath: s`${srcPath}`
},
code: code
} as BsDiagnostic;
Expand Down
4 changes: 2 additions & 2 deletions src/DiagnosticFilterer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ export class DiagnosticFilterer {

for (let diagnostic of diagnostics) {
//skip diagnostics that have issues
if (!diagnostic?.file?.pathAbsolute) {
if (!diagnostic?.file?.srcPath) {
continue;
}
const lowerSrcPath = diagnostic.file.pathAbsolute.toLowerCase();
const lowerSrcPath = diagnostic.file.srcPath.toLowerCase();
//make a new array for this file if one does not yet exist
if (!this.byFile[lowerSrcPath]) {
this.byFile[lowerSrcPath] = [];
Expand Down
49 changes: 23 additions & 26 deletions src/LanguageServer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { standardizePath as s, util } from './util';
import { TextDocument } from 'vscode-languageserver-textdocument';
import type { Program } from './Program';
import * as assert from 'assert';
import type { XmlFile } from './files/XmlFile';

let sinon: sinonImport.SinonSandbox;
beforeEach(() => {
Expand Down Expand Up @@ -80,11 +81,11 @@ describe('LanguageServer', () => {

//hijack the file resolver so we can inject in-memory files for our tests
let originalResolver = svr.documentFileResolver;
svr.documentFileResolver = (pathAbsolute: string) => {
if (vfs[pathAbsolute]) {
return vfs[pathAbsolute];
svr.documentFileResolver = (srcPath: string) => {
if (vfs[srcPath]) {
return vfs[srcPath];
} else {
return originalResolver.call(svr, pathAbsolute);
return originalResolver.call(svr, srcPath);
}
};

Expand All @@ -96,7 +97,7 @@ describe('LanguageServer', () => {
afterEach(async () => {
try {
await Promise.all(
physicalFilePaths.map(pathAbsolute => fsExtra.unlinkSync(pathAbsolute))
physicalFilePaths.map(srcPath => fsExtra.unlinkSync(srcPath))
);
} catch (e) {

Expand All @@ -116,21 +117,17 @@ describe('LanguageServer', () => {
}

function addScriptFile(name: string, contents: string, extension = 'brs') {
const filePath = s`components/${name}.${extension}`;
program.addOrReplaceFile(filePath, contents);
for (const key in program.files) {
if (key.includes(filePath)) {
const document = TextDocument.create(util.pathToUri(key), 'brightscript', 1, contents);
svr.documents._documents[document.uri] = document;
return document;
}
}
const pkgPath = `pkg:/components/${name}.${extension}`;
const file = program.addOrReplaceFile<XmlFile>(pkgPath, contents);
const document = TextDocument.create(util.pathToUri(file.srcPath), 'brightscript', 1, contents);
svr.documents._documents[document.uri] = document;
return document;
}

function writeToFs(pathAbsolute: string, contents: string) {
physicalFilePaths.push(pathAbsolute);
fsExtra.ensureDirSync(path.dirname(pathAbsolute));
fsExtra.writeFileSync(pathAbsolute, contents);
function writeToFs(srcPath: string, contents: string) {
physicalFilePaths.push(srcPath);
fsExtra.ensureDirSync(path.dirname(srcPath));
fsExtra.writeFileSync(srcPath, contents);
}

describe('createStandaloneFileWorkspace', () => {
Expand Down Expand Up @@ -193,7 +190,7 @@ describe('LanguageServer', () => {
getDiagnostics: () => {
return [{
file: {
pathAbsolute: s`${rootDir}/source/main.brs`
srcPath: s`${rootDir}/source/main.brs`
},
code: 1000,
range: Range.create(1, 2, 3, 4)
Expand All @@ -206,7 +203,7 @@ describe('LanguageServer', () => {
getDiagnostics: () => {
return [{
file: {
pathAbsolute: s`${rootDir}/source/main.brs`
srcPath: s`${rootDir}/source/main.brs`
},
code: 1000,
range: Range.create(1, 2, 3, 4)
Expand Down Expand Up @@ -294,7 +291,7 @@ describe('LanguageServer', () => {

await server.handleFileChanges(workspace, [{
type: FileChangeType.Created,
pathAbsolute: mainPath
srcPath: mainPath
}]);

expect(addOrReplaceFileStub.getCalls()[0]?.args[0]).to.eql({
Expand All @@ -307,7 +304,7 @@ describe('LanguageServer', () => {
expect(addOrReplaceFileStub.callCount).to.equal(1);
await server.handleFileChanges(workspace, [{
type: FileChangeType.Created,
pathAbsolute: libPath
srcPath: libPath
}]);
//the function should have ignored the lib file, so no additional files were added
expect(addOrReplaceFileStub.callCount).to.equal(1);
Expand Down Expand Up @@ -347,10 +344,10 @@ describe('LanguageServer', () => {

expect(stub.getCalls()[0].args[1]).to.eql([{
type: FileChangeType.Created,
pathAbsolute: s`${rootDir}/source/main.brs`
srcPath: s`${rootDir}/source/main.brs`
}, {
type: FileChangeType.Created,
pathAbsolute: s`${rootDir}/source/lib.brs`
srcPath: s`${rootDir}/source/lib.brs`
}]);
});

Expand Down Expand Up @@ -385,10 +382,10 @@ describe('LanguageServer', () => {

expect(stub.getCalls()[0].args[1]).to.eql([{
type: FileChangeType.Created,
pathAbsolute: s`${rootDir}/source/main.brs`
srcPath: s`${rootDir}/source/main.brs`
}, {
type: FileChangeType.Created,
pathAbsolute: s`${rootDir}/source/lib.brs`
srcPath: s`${rootDir}/source/lib.brs`
}]);
});
});
Expand Down
Loading

0 comments on commit 0c0b97e

Please sign in to comment.