Skip to content

Commit

Permalink
build: Make default editor configurable. Defaults to VS code.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmiscool committed Dec 14, 2024
1 parent 62dfd2a commit dd3cf38
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 30 deletions.
5 changes: 3 additions & 2 deletions src/aiCoderApiFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getListOfFunctions, getMethodsWithArguments } from "./classListing.js";
import { appendFile, getAllFiles, moveFile, readFile, readOrLoadFromDefault, readSetting, writeFile, writeSetting } from "./fileIO.js";
import { intelligentlyMergeSnippets } from "./intelligentMerge.js";
import { llmSettings, llmSettingsUpdate } from "./llmCall.js";
import { launchNano } from "./terminalHelpers.js";
import { launchEditor } from "./terminalHelpers.js";
import { prependClassStructure } from './classListing.js';
import fs from 'fs';
import { callLLM } from './llmCall.js';
Expand Down Expand Up @@ -167,7 +167,8 @@ export class aiCoderApiFunctions {
}

async gotoLineNumber(parsedBody) {
await launchNano(parsedBody.targetFile, parsedBody.lineNumber);
console.log('gotoLineNumber', parsedBody);
await launchEditor(parsedBody.targetFile, parsedBody.lineNumber);
return { success: true };
}

Expand Down
4 changes: 2 additions & 2 deletions src/classListing.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function prependClassStructure(targetFile, onlyStubs = false) {

const fileContent = await readFile(targetFile);
const list = await getMethodsWithArguments(fileContent, onlyStubs);
console.log(list);
//console.log(list);

// Prompt if the user wants to include function names in the list
const includeFunctions = true;
Expand Down Expand Up @@ -73,7 +73,7 @@ export function getMethodsWithArguments(code, onlyStubs = false) {
if (classElement.type === 'MethodDefinition' && classElement.key.type === 'Identifier') {
const methodName = classElement.key.name;
const lineNumber = classElement.loc.start.line;
console.log(methodName,lineNumber);
//console.log(methodName,lineNumber);

const args = classElement.value.params.map(param => {
if (param.type === 'Identifier') return param.name;
Expand Down
16 changes: 12 additions & 4 deletions src/fileIO.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path, { dirname } from 'path';
import { createBackup } from './backupSystem.js';
import { pressEnterToContinue, printAndPause, printDebugMessage } from './terminalHelpers.js';
import { printAndPause, printDebugMessage } from './terminalHelpers.js';
import { fileURLToPath } from 'url';

// Helper functions to read, write, and append to files
Expand All @@ -27,8 +27,6 @@ export async function writeFile(filePath, content, makeBackup = false) {
if (typeof content !== 'string') {
await printAndPause('Content is not a string:');
await printAndPause(content);
await pressEnterToContinue();

}
// create the folders in the file path if they don't exist
let folderPath = path.dirname(filePath);
Expand Down Expand Up @@ -93,10 +91,20 @@ export async function readOrLoadFromDefault(filePath, defaultFilePath = null) {
export async function readSetting(fileName) {
// by default the settings are stored in the ./.aiCoder folder.
// If the file is not found we will use the readOrLoadFromDefault function to create a new file with the default settings
return await readOrLoadFromDefault(`./.aiCoder/${fileName}`, `/${fileName}`);

try {
return await readOrLoadFromDefault(`./.aiCoder/${fileName}`, `/${fileName}`);
} catch (error) {
console.log('Error reading setting:', fileName);
//console.log('Error:', error);
return null;
}


}

export async function writeSetting(fileName, content) {
console.log('Writing setting:', fileName, content);
// by default the settings are stored in the ./.aiCoder folder.
await writeFile(`./.aiCoder/${fileName}`, content);
}
Expand Down
93 changes: 71 additions & 22 deletions src/terminalHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { exec } from "child_process";
import path from 'path';
import { ctx, debugMode } from './main.js';
import { wss } from './apiServer.js';

import { readSetting, writeSetting } from "./fileIO.js";
import { read, write } from "fs";



Expand Down Expand Up @@ -90,55 +91,103 @@ export async function selectListHeight() {




export async function input(promptText, defaultValue = '') {
let promptObject = {};
promptObject.type = 'input';
promptObject.name = 'input';
promptObject.message = promptText || 'Enter a value:';
promptObject.default = defaultValue || '';
return await (await inquirer.prompt([promptObject])).input;
export async function launchEditor(filePath, lineNumber = null) {
// load the default editor from the setting
const editorToUse = await readSetting('preferredEditor.txt');
printAndPause(`Opening ${filePath} to line ${lineNumber} with ${editorToUse}`);
if (editorToUse === 'vscode') return launchVScode(filePath, lineNumber);
if (editorToUse === 'neovim') return launchNeovim(filePath, lineNumber);
if (editorToUse === 'nano') return launchNano(filePath, lineNumber);
if (editorToUse === 'vim') return launchVim(filePath, lineNumber);
return launchVScode(filePath, lineNumber);
}


export async function confirmAction(message, defaultValue = true) {
let promptObject = {};
promptObject.type = 'confirm';
promptObject.name = 'confirm';
promptObject.message = message || 'Are you sure?';
promptObject.default = defaultValue;
return await (await inquirer.prompt([promptObject])).confirm;
async function setEditor() {
if (readArg('-editor')) {
console.log('Prefered editor is set to:', await readSetting('preferredEditor.txt'));
console.log('Options are: vscode, neovim, nano, vim');
if (readArg('-editor') === 'vscode') await writeSetting('preferredEditor.txt', 'vscode');
if (readArg('-editor') === 'neovim') await writeSetting('preferredEditor.txt', 'neovim');
if (readArg('-editor') === 'nano') await writeSetting('preferredEditor.txt', 'nano');
if (readArg('-editor') === 'vim') await writeSetting('preferredEditor.txt', 'vim');
process.exit(0);
}
}
setEditor();


export function launchNano(filePath, lineNumber = null) {
export function launchVScode(filePath, lineNumber = null) {
console.log('launchNano', filePath, lineNumber);

// convert file path to absolute path
filePath = path.resolve(filePath);


const stringCommand = `code -g ${filePath}:${lineNumber}:1`;;
const stringCommand = `code -g ${filePath}:${lineNumber}:1`;
console.log('stringCommand', stringCommand);
return exec(stringCommand, (error) => {
if (error) {
console.error('Error opening the file with VSCode:', error);
}
});
}


export function launchNeovim(filePath, lineNumber = null) {
// Convert file path to absolute path
filePath = path.resolve(filePath);

// Construct the command string for Neovim
const lineArgument = lineNumber ? `+${lineNumber}` : '';
const stringCommand = `nvim ${lineArgument} ${filePath}`;
console.log('stringCommand', stringCommand);

return exec(stringCommand, (error) => {
if (error) {
console.error('Error opening the file with Neovim:', error);
}
});
}

// launch regular vim
export function launchVim(filePath, lineNumber = null) {
// Convert file path to absolute path
filePath = path.resolve(filePath);

// Construct the command string for Neovim
const lineArgument = lineNumber ? `+${lineNumber}` : '';
const stringCommand = `vim ${lineArgument} ${filePath}`;
console.log('stringCommand', stringCommand);

return exec(stringCommand, (error) => {
if (error) {
console.error('Error opening the file with Vim:', error);
}
});
}


export function launchNano(filePath, lineNumber = null) {
// Convert file path to absolute path
filePath = path.resolve(filePath);

// function to press any key to continue
export async function pressEnterToContinue() {
return await input('Press Enter to continue...');
// Construct the command string for Nano
const lineArgument = lineNumber ? `+${lineNumber}` : '';
const stringCommand = `nano ${lineArgument} ${filePath}`;
console.log('stringCommand', stringCommand);

return exec(stringCommand, (error) => {
if (error) {
console.error('Error opening the file with Nano:', error);
}
});
}






export async function printDebugMessage(message) {
if (debugMode) {
const stack = new Error().stack;
Expand Down

0 comments on commit dd3cf38

Please sign in to comment.