diff --git a/lib/elixir-jump-around.js b/lib/elixir-jump-around.js index 472788e..2e697b9 100644 --- a/lib/elixir-jump-around.js +++ b/lib/elixir-jump-around.js @@ -2,11 +2,18 @@ import { CompositeDisposable } from 'atom'; const path = require('path'); +const fs = require('fs'); export default { subscriptions: null, + // Should change these to workspace settings + folders: { + lib: "lib", + test: "test/lib", + }, + activate(state) { // Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable this.subscriptions = new CompositeDisposable(); @@ -21,28 +28,52 @@ export default { this.subscriptions.dispose(); }, + + + + + toggle_test_file() { - currentFilePath = atom.workspace.getActiveTextEditor().getPath(); - opts = {searchAllPanes: true} + const currentFilePath = atom.workspace.getActiveTextEditor().getPath(); + const opts = {searchAllPanes: true} if (this.isTestFilePath(currentFilePath)) { return atom.workspace.open(this.getModulePath(currentFilePath), opts); } else { - return atom.workspace.open(this.getTestPath(currentFilePath), opts); + return atom.workspace.open(this.getTestPath(currentFilePath), opts) + .then(editor => { + if(editor.isEmpty()) { + return editor.setText(this.template(currentFilePath)) + } + return editor + }) } }, isTestFilePath(filePath) { return atom.project.getPaths().find((rootPath) => { - relativePath = path.relative(rootPath, filePath); + const relativePath = path.relative(rootPath, filePath); return relativePath.match(/test\/.+_test\.exs$/); }); }, getModulePath(filePath) { - return filePath.replace("test", "lib").replace(/_test\.exs$/, ".ex"); + return filePath.replace(this.folders.test, this.folders.lib).replace(/_test\.exs$/, ".ex"); + }, + + getTestPath(filePath) {`` + return filePath.replace(this.folders.lib, this.folders.test).replace(/\.ex$/, "_test.exs"); + }, + + getModuleName(filePath) { + const contents = fs.readFileSync(filePath, 'utf8'); + const matches = contents.match(/defmodule\s+(.+)\s+do\s/) + return matches[0] }, - getTestPath(filePath) { - return filePath.replace("lib", "test").replace(/\.ex$/, "_test.exs"); + template(currentFilePath) { + const moduleFilePath = this.getModulePath(currentFilePath); + const module = getModuleName(moduleFilePath) + const relativePath = path.relative(rootPath, filePath); + return `defmodule ${module}Test do\n describe ".method/0" do\n test "with valid params" do\n raise "Not Implemented"\n end\n end\nend\n` } };