From 8730b032946506b90a702a337c70325e782e4665 Mon Sep 17 00:00:00 2001 From: Maciej Wilk Date: Thu, 25 Aug 2022 09:21:20 +0000 Subject: [PATCH 1/3] add alternateFileType setting and use it --- Scripts/commands/RailsAlternateFile.js | 12 ++++--- Scripts/settings.js | 2 ++ Scripts/settings/general/alternateFileType.js | 32 +++++++++++++++++++ extension.json | 18 +++++++++++ 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 Scripts/settings/general/alternateFileType.js diff --git a/Scripts/commands/RailsAlternateFile.js b/Scripts/commands/RailsAlternateFile.js index 190ecdb..f2ec742 100644 --- a/Scripts/commands/RailsAlternateFile.js +++ b/Scripts/commands/RailsAlternateFile.js @@ -1,3 +1,5 @@ +const SETTINGS = require("../settings") + exports.RailsAlternateFile = class RailsAlternateFile { constructor() { // FIXME: Handle running the command while something else than a file is active (like a terminal) @@ -7,20 +9,22 @@ exports.RailsAlternateFile = class RailsAlternateFile { // TODO: Refactor and improve code quality alternate() { + var testType = (SETTINGS.general.alternateFileType == "rspec" ? "spec" : "test"); + // If neither app nor test are found in the path, can't find associated file - if (this.splitPath.indexOf("app") === -1 && this.splitPath.indexOf("test") === -1) { + if (this.splitPath.indexOf("app") === -1 && this.splitPath.indexOf(testType) === -1) { this.showError("Couldn't find alternate files. Does the workspace contain a Rails project?") return } - const rootIndex = this.splitPath.indexOf("app") === -1 ? this.splitPath.indexOf("test") : this.splitPath.indexOf("app") + const rootIndex = this.splitPath.indexOf("app") === -1 ? this.splitPath.indexOf(testType) : this.splitPath.indexOf("app") const rootDir = this.splitPath[rootIndex] - const associatedDir = rootDir === "app" ? "test" : "app" + const associatedDir = rootDir === "app" ? testType : "app" const currentFileName = this.splitPath[this.splitPath.length - 1] let associatedFileName - if (associatedDir === "test") { + if (associatedDir === testType) { associatedFileName = currentFileName.slice(0, -3).concat("_test.rb") } else { associatedFileName = currentFileName.replace("_test", "") diff --git a/Scripts/settings.js b/Scripts/settings.js index 9aea483..1e80397 100644 --- a/Scripts/settings.js +++ b/Scripts/settings.js @@ -1,5 +1,6 @@ // EXTENSION const { statusNotifications } = require("./settings/general/statusNotifications") +const { alternateFileType } = require("./settings/general/alternateFileType") // SOLARGRAPH const { autoFormat } = require("./settings/solargraph/autoFormat") const { bundlerPath } = require("./settings/solargraph/bundlerPath") @@ -23,6 +24,7 @@ const { autocorrectDisableUncorrectable } = require("./settings/rubocop/autocorr module.exports = { statusNotifications, + alternateFileType, solargraph: { autoFormat, bundlerPath, diff --git a/Scripts/settings/general/alternateFileType.js b/Scripts/settings/general/alternateFileType.js new file mode 100644 index 0000000..86a1dae --- /dev/null +++ b/Scripts/settings/general/alternateFileType.js @@ -0,0 +1,32 @@ +function reload() { + nova.commands.invoke("tommasonegri.rails.commands.reload") +} + +nova.config.onDidChange("tommasonegri.rails.config.general.alternateFileType", reload) +nova.workspace.config.onDidChange("tommasonegri.rails.config.general.alternateFileType", reload) + +function getExtensionSetting() { + return nova.config.get("tommasonegri.rails.config.general.alternateFileType", "boolean") +} + +function getWorkspaceSetting() { + const str = nova.workspace.config.get("tommasonegri.rails.config.general.alternateFileType", "string") + + switch (str) { + case "Global Default": + return null + case "minitest": + return "minitest" + case "rspec": + return "rspec" + default: + return null + } +} + +exports.statusNotialternateFileTypefications = function() { + const workspaceConfig = getWorkspaceSetting() + const extensionConfig = getExtensionSetting() + + return workspaceConfig === null ? extensionConfig : workspaceConfig +} diff --git a/extension.json b/extension.json index 104b6c7..0316c4d 100644 --- a/extension.json +++ b/extension.json @@ -385,6 +385,15 @@ "type": "boolean", "default": true }, + { + "key": "tommasonegri.rails.config.general.alternateFileType", + "title": "Alternate File type", + "description": "Select whether 'Open Alternate File' should use minitest or rspec.", + "type": "enum", + "values": ["minitest", "rspec"], + "radio": false, + "default": "minitest" + }, { "title": "Solargraph", "description": "Solargraph is a Ruby language server and suite of static analysis tools. The language server provides intellisense, autocompletion, diagnostics, and other language features for editors and IDEs with language client capabilities. The static analysis tools check code for type safety.", @@ -557,6 +566,15 @@ "radio": false, "default": "Global Default" }, + { + "key": "tommasonegri.rails.config.general.alternateFileType", + "title": "Alternate File type", + "description": "Select whether 'Open Alternate File' should use minitest or rspec.", + "type": "enum", + "values": ["Global Default", "minitest", "rspec"], + "radio": false, + "default": "minitest" + }, { "title": "Solargraph settings", "description": "Solargraph is a Ruby language server and suite of static analysis tools. The language server provides intellisense, autocompletion, diagnostics, and other language features for editors and IDEs with language client capabilities. The static analysis tools check code for type safety.", From f02641a2c366c4964f73d18bc2a73d1b651fa106 Mon Sep 17 00:00:00 2001 From: Maciej Wilk Date: Thu, 25 Aug 2022 09:23:51 +0000 Subject: [PATCH 2/3] fix interpolation --- Scripts/commands/RailsAlternateFile.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Scripts/commands/RailsAlternateFile.js b/Scripts/commands/RailsAlternateFile.js index f2ec742..6fdf8ef 100644 --- a/Scripts/commands/RailsAlternateFile.js +++ b/Scripts/commands/RailsAlternateFile.js @@ -25,9 +25,9 @@ exports.RailsAlternateFile = class RailsAlternateFile { let associatedFileName if (associatedDir === testType) { - associatedFileName = currentFileName.slice(0, -3).concat("_test.rb") + associatedFileName = currentFileName.slice(0, -3).concat("_${testType}.rb") } else { - associatedFileName = currentFileName.replace("_test", "") + associatedFileName = currentFileName.replace("_${testType}", "") } let newSplitPath = this.splitPath From 8c68ca1e3a7a6ae6fe7fdf0ed4dd4eb024e07c48 Mon Sep 17 00:00:00 2001 From: Maciej Wilk Date: Thu, 25 Aug 2022 09:27:25 +0000 Subject: [PATCH 3/3] fix global default --- extension.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension.json b/extension.json index 0316c4d..9e9382d 100644 --- a/extension.json +++ b/extension.json @@ -573,7 +573,7 @@ "type": "enum", "values": ["Global Default", "minitest", "rspec"], "radio": false, - "default": "minitest" + "default": "Global Default" }, { "title": "Solargraph settings",