Skip to content

Commit

Permalink
moved getTempFile func to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
d-mitrofanov-v committed Sep 28, 2023
1 parent 6dcb762 commit be0103f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 44 deletions.
28 changes: 1 addition & 27 deletions src/features/refactor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ChildProcess } from 'child_process';
import { Disposable, Document, OutputChannel, Position, Range, TextDocument, Uri, window, workspace } from 'coc.nvim';
import * as path from 'path';
import md5 from 'md5';
import fs from 'fs';
import { createDeferred, Deferred } from '../async';
import { PythonSettings } from '../configSettings';
import { PythonExecutionService } from '../processService';
import { IPythonSettings } from '../types';
import { getTextEditsFromPatch, getWindowsLineEndingCount, splitLines } from '../utils';
import { getTextEditsFromPatch, getWindowsLineEndingCount, splitLines, getTempFileWithDocumentContents } from '../utils';

class RefactorProxy implements Disposable {
protected readonly isWindows = process.platform === 'win32';
Expand Down Expand Up @@ -194,18 +193,6 @@ interface RenameResponse {
results: [{ diff: string }];
}

async function checkDocument(doc: Document): Promise<boolean> {
if (!doc) return false;

const modified = await doc.buffer.getOption('modified');
if (modified != 0) {
window.showWarningMessage('Buffer not saved, please save the buffer first!');
return false;
}

return true;
}

function validateDocumentForRefactor(doc: Document): Promise<void> {
if (!doc.dirty) {
return Promise.resolve();
Expand All @@ -218,19 +205,6 @@ function validateDocumentForRefactor(doc: Document): Promise<void> {
});
}

function getTempFileWithDocumentContents(document: TextDocument): Promise<string> {
return new Promise<string>((resolve, reject) => {
const fsPath = Uri.parse(document.uri).fsPath;
const fileName = `${fsPath.slice(0, -3)}${md5(document.uri)}${path.extname(fsPath)}`;
fs.writeFile(fileName, document.getText(), (ex) => {
if (ex) {
reject(new Error(`Failed to create a temporary file, ${ex.message}`));
}
resolve(fileName);
});
});
}

export async function extractVariable(root: string, document: TextDocument, range: Range, outputChannel: OutputChannel): Promise<any> {
const doc = workspace.getDocument(document.uri);
const tempFile = await getTempFileWithDocumentContents(document);
Expand Down
18 changes: 2 additions & 16 deletions src/features/sortImports.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
import { OutputChannel, TextDocument, Uri, window, workspace } from 'coc.nvim';
import { OutputChannel, TextDocument, window, workspace } from 'coc.nvim';
import fs from 'fs';
import md5 from 'md5';
import * as path from 'path';
import which from 'which';
import { PythonSettings } from '../configSettings';
import { PythonExecutionService } from '../processService';
import { ExecutionInfo } from '../types';
import { getTextEditsFromPatch } from '../utils';

function getTempFileWithDocumentContents(document: TextDocument): Promise<string> {
return new Promise<string>((resolve, reject) => {
const fsPath = Uri.parse(document.uri).fsPath;
const fileName = `${fsPath}.${md5(document.uri)}${path.extname(fsPath)}`;
fs.writeFile(fileName, document.getText(), (ex) => {
if (ex) {
reject(new Error(`Failed to create a temporary file, ${ex.message}`));
}
resolve(fileName);
});
});
}
import { getTextEditsFromPatch, getTempFileWithDocumentContents } from '../utils';

function getSortProviderInfo(provider: 'pyright' | 'isort' | 'ruff', extensionRoot: string): ExecutionInfo | null {
const pythonSettings = PythonSettings.getInstance();
Expand Down
19 changes: 18 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { Position, Range, TextDocument, TextEdit } from 'coc.nvim';
import { Position, Range, TextDocument, TextEdit, Uri } from 'coc.nvim';
import { Diff, diff_match_patch } from 'diff-match-patch';
import fs from 'fs';
import md5 from 'md5';
import { EOL } from 'os';
import * as path from 'path';
const NEW_LINE_LENGTH = EOL.length;

enum EditAction {
Expand Down Expand Up @@ -263,3 +266,17 @@ export function getWindowsLineEndingCount(document: TextDocument, offset: number
}
return count;
}

export function getTempFileWithDocumentContents(document: TextDocument): Promise<string> {
return new Promise<string>((resolve, reject) => {
const fsPath = Uri.parse(document.uri).fsPath;
const fileName = `${fsPath.slice(0, -3)}${md5(document.uri)}${path.extname(fsPath)}`;
fs.writeFile(fileName, document.getText(), (ex) => {
if (ex) {
reject(new Error(`Failed to create a temporary file, ${ex.message}`));
}
resolve(fileName);
});
});
}

0 comments on commit be0103f

Please sign in to comment.