Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update elixir-jump-around.js #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions lib/elixir-jump-around.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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`
}
};