Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc file tweaks #329

Closed
wants to merge 9 commits into from
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 @@ -153,7 +153,7 @@ interface FileObj {
}

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

Expand Down
7 changes: 3 additions & 4 deletions src/DiagnosticCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { BsDiagnostic } from '.';
import { DiagnosticCollection } from './DiagnosticCollection';
import type { Workspace } from './LanguageServer';
import type { ProgramBuilder } from './ProgramBuilder';
import type { File } from './interfaces';
import util from './util';
import { expect } from 'chai';

Expand Down Expand Up @@ -87,7 +86,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,8 +98,8 @@ describe('DiagnosticCollection', () => {
for (const message of messages) {
diagnostics.push({
file: {
pathAbsolute: filePath
} as File,
srcPath: filePath
},
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
code: 123,
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 @@ -148,10 +148,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
10 changes: 5 additions & 5 deletions src/DiagnosticFilterer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ export class DiagnosticFilterer {

for (let diagnostic of diagnostics) {
//skip diagnostics that have issues
if (!diagnostic?.file?.pathAbsolute) {
if (!diagnostic?.file?.srcPath) {
continue;
}
//make a new array for this file if one does not yet exist
if (!this.byFile[diagnostic.file.pathAbsolute]) {
this.byFile[diagnostic.file.pathAbsolute] = [];
if (!this.byFile[diagnostic.file.srcPath]) {
this.byFile[diagnostic.file.srcPath] = [];
}
this.byFile[diagnostic.file.pathAbsolute].push(diagnostic);
this.filePaths.push(diagnostic.file.pathAbsolute);
this.byFile[diagnostic.file.srcPath].push(diagnostic);
this.filePaths.push(diagnostic.file.srcPath);
}
}

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