Skip to content

Commit

Permalink
refactor: fileProvider supports VirtualDocument with Uri to real File
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed May 29, 2021
1 parent b772c49 commit 29bfae2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/documentStore.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { HttpFileStore, HttpFile } from 'httpyac';
import { HttpFileStore, HttpFile, PathLike } from 'httpyac';
import { TextDocument } from 'vscode';


export class DocumentStore {

constructor(readonly httpFileStore: HttpFileStore) { }
public getDocumentPathLike: (document: TextDocument) => PathLike;

constructor(readonly httpFileStore: HttpFileStore) {
this.getDocumentPathLike = document => document.uri;
}

getHttpFile(document: TextDocument): Promise<HttpFile> {
return this.httpFileStore.getOrCreate(document.uri, () => Promise.resolve(document.getText()), document.version);
return this.httpFileStore.getOrCreate(this.getDocumentPathLike(document), () => Promise.resolve(document.getText()), document.version);
}

getAll(): Array<HttpFile> {
Expand Down
6 changes: 4 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ export function activate(context: vscode.ExtensionContext): HttpYacExtensionApi
}
} else if (vscode.workspace.workspaceFolders) {
for (const workspaceFolder of vscode.workspace.workspaceFolders) {
const file = httpyac.fileProvider.joinPath(workspaceFolder.uri, fileName);
const file = vscode.Uri.joinPath(workspaceFolder.uri, fileName);
try {
const script = await httpyac.fileProvider.readFile(file, 'utf-8');
const content = await vscode.workspace.fs.readFile(file);
const script = Buffer.from(content).toString('utf-8');
return {
script,
lineOffset: 0
Expand All @@ -92,6 +93,7 @@ export function activate(context: vscode.ExtensionContext): HttpYacExtensionApi
return {
httpyac,
httpFileStore,
documentStore,
responseHandlers,
responseOutputProcessor,
httpDocumentSelector: config.httpDocumentSelector,
Expand Down
2 changes: 2 additions & 0 deletions src/extensionApi.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as vscode from 'vscode';
import * as httpyac from 'httpyac';
import { ResponseOutputProcessor, ResponseHandler } from './view';
import { DocumentStore } from './documentStore';

export interface HttpYacExtensionApi{
httpyac: typeof httpyac,
responseHandlers: Array<ResponseHandler>,
responseOutputProcessor: ResponseOutputProcessor,
httpFileStore: httpyac.HttpFileStore,
documentStore: DocumentStore,
httpDocumentSelector: vscode.DocumentSelector,
refreshCodeLens: vscode.EventEmitter<void>,
environementChanged: vscode.EventEmitter<string[] | undefined>
Expand Down
33 changes: 19 additions & 14 deletions src/fileProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { workspace, Uri } from 'vscode';
import { fileProvider, FileEnconding, WatchDispose, PathLike, log } from 'httpyac';

export function initVscodeFileProvider(): void {


export function initVscodeFileProvider(): void {
fileProvider.dirname = (fileName: string) => {
const uri = toUri(fileName);
if (uri) {
Expand All @@ -19,13 +18,6 @@ export function initVscodeFileProvider(): void {
}
throw new Error('No valid uri');
};
fileProvider.toString = (fileName: PathLike): string => {
const uri = toUri(fileName);
if (uri) {
return uri.toString();
}
throw new Error('No valid uri');
};

fileProvider.exists = async (fileName: PathLike): Promise<boolean> => {
try {
Expand Down Expand Up @@ -78,13 +70,26 @@ export function initVscodeFileProvider(): void {

}

interface VirtualDocument{
uri: Uri,
fileUri: Uri;
toString(): string;
}

function toUri(fileName: PathLike): Uri | false {
if (typeof fileName === 'string') {
return Uri.file(fileName);
function toUri(pathLike: PathLike): Uri | false {
if (typeof pathLike === 'string') {
return Uri.file(pathLike);
}
if (fileName instanceof Uri) {
return fileName;
if (pathLike instanceof Uri) {
return pathLike;
}
if (isVirtualDocument(pathLike)) {
return pathLike.fileUri || pathLike.uri;
}
return false;
}

function isVirtualDocument(pathLike: PathLike): pathLike is VirtualDocument {
const virtualDocument = pathLike as VirtualDocument;
return !!virtualDocument.uri;
}

0 comments on commit 29bfae2

Please sign in to comment.