fish-lsp
Version:
LSP implementation for fish/fish-shell
113 lines (112 loc) • 4.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SyncFileHelper = void 0;
const fs_1 = require("fs");
const vscode_languageserver_1 = require("vscode-languageserver");
const document_1 = require("../document");
const translation_1 = require("./translation");
const path_1 = require("path");
/**
* Synchronous file operations.
*/
class SyncFileHelper {
static open(filePath, flags) {
const expandedFilePath = this.expandEnvVars(filePath);
return (0, fs_1.openSync)(expandedFilePath, flags);
}
static close(fd) {
(0, fs_1.closeSync)(fd);
}
static read(filePath, encoding = 'utf8') {
const expandedFilePath = this.expandEnvVars(filePath);
return (0, fs_1.readFileSync)(expandedFilePath, { encoding });
}
static write(filePath, data, encoding = 'utf8') {
const expandedFilePath = this.expandEnvVars(filePath);
(0, fs_1.writeFileSync)(expandedFilePath, data, { encoding });
}
static append(filePath, data, encoding = 'utf8') {
const expandedFilePath = this.expandEnvVars(filePath);
(0, fs_1.appendFileSync)(expandedFilePath, data, { encoding });
}
static expandEnvVars(filePath) {
let filePathString = filePath.toString();
// Expand ~ to home directory
filePathString = filePathString.replace(/^~/, process.env.HOME);
// Expand environment variables
filePathString = filePathString.replace(/\$([a-zA-Z0-9_]+)/g, (_, envVarName) => {
return process.env[envVarName] || '';
});
return filePathString;
}
static exists(filePath) {
const expandedFilePath = this.expandEnvVars(filePath);
return (0, fs_1.existsSync)(expandedFilePath);
}
static delete(filePath) {
(0, fs_1.unlinkSync)(filePath);
}
static create(filePath) {
const expandedFilePath = this.expandEnvVars(filePath);
if (this.isDirectory(expandedFilePath)) {
return this.getPathTokens(filePath);
}
else if (!this.exists(expandedFilePath)) {
this.write(expandedFilePath, '');
}
return this.getPathTokens(expandedFilePath);
}
static getPathTokens(filePath) {
const expandedFilePath = this.expandEnvVars(filePath);
return {
path: expandedFilePath,
filename: (0, path_1.basename)(expandedFilePath, (0, path_1.extname)(expandedFilePath)),
extension: (0, path_1.extname)(expandedFilePath).substring(1),
directory: (0, path_1.dirname)(expandedFilePath),
exists: this.exists(expandedFilePath),
uri: (0, translation_1.pathToUri)(expandedFilePath),
};
}
static convertTextToFishFunction(filePath, data, _encoding = 'utf8') {
const expandedFilePath = this.expandEnvVars(filePath);
const { filename, path, extension, exists } = this.getPathTokens(expandedFilePath);
const content = [
'',
`function ${filename}`,
data.split('\n').map(line => '\t' + line).join('\n'),
'end',
].join('\n');
if (exists) {
this.append(path, content, 'utf8');
return this.toLspDocument(path, extension, 1);
}
this.write(path, content);
return this.toLspDocument(path, extension, 1);
}
static toTextDocumentItem(filePath, languageId, version) {
const expandedFilePath = this.expandEnvVars(filePath);
const content = this.read(expandedFilePath);
const uri = (0, translation_1.pathToUri)(expandedFilePath.toString());
return vscode_languageserver_1.TextDocumentItem.create(uri, languageId, version, content);
}
static toLspDocument(filePath, languageId, version) {
const expandedFilePath = this.expandEnvVars(filePath);
let content = this.read(expandedFilePath);
if (!content) {
content = '';
}
const doc = this.toTextDocumentItem(expandedFilePath, languageId, version);
return new document_1.LspDocument(doc);
}
static isDirectory(filePath) {
const expandedFilePath = this.expandEnvVars(filePath);
try {
const fileStat = (0, fs_1.statSync)(expandedFilePath);
return fileStat.isDirectory();
}
catch (_) {
return false;
}
}
}
exports.SyncFileHelper = SyncFileHelper;