Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
zedtux committed Aug 9, 2016
0 parents commit 9b525e9
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
npm-debug.log
node_modules
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0 - First Release
* Every feature added
* Every bug fixed
20 changes: 20 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2016 Guillaume Hain

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Ruby On Rails Refactor package

A set of refactoring tools for Ruby On Rails.
It should ease your life.

## Refactoring tools

This lists the available refactoring tools installed by this package:

- Extract Method (Move a piece of code in a new method)

## Installation

You can install it from the console with:

```bash
$ apm install ror-refactor
```

Or from Atom itself.

## Usage

#### Extract Method

Select a bunch of code then hit `CTRL + ALT + CMD + R`:

<INSERT GIF HERE>

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
5 changes: 5 additions & 0 deletions keymaps/ror-refactor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"atom-workspace": {
"ctrl-alt-cmd-r": "ror-refactor:extract-method"
}
}
94 changes: 94 additions & 0 deletions lib/ror-refactor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use babel';

import { CompositeDisposable } from 'atom';

export default {

subscriptions: null,
extractedMethod: null,
extractedMethodPosition: null,
rowCountBetweenExtractedCodeAndDef: 0,

activate(state) {
// Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
this.subscriptions = new CompositeDisposable();

// Register command that toggles this view
this.subscriptions.add(atom.commands.add('atom-workspace', {
'ror-refactor:extract-method': () => this.extractMethod()
}));
},

deactivate() {
this.subscriptions.dispose();
},

serialize() {
},

extractMethod() {
var editor = atom.workspace.getActiveTextEditor();
var cursor = editor.getLastCursor();
var grammar = editor.getGrammar();

this.extractMethodFetchBody(editor);

var i, ref, rowNumber, methodDefIndentation;
var methodDefFound = false;
for (rowNumber = i = ref = cursor.getBufferRow(); ref <= 0 ? i <= 0 : i >= 0; rowNumber = ref <= 0 ? ++i : --i) {
this.rowCountBetweenExtractedCodeAndDef++;
// Ignore comments in and out of the current method
if (editor.isBufferRowCommented(rowNumber)) {
continue;
} else {
var row = editor.lineTextForBufferRow(rowNumber);
var tokens = grammar.tokenizeLine(row).tokens;

// Detect the 'def' of the current method
if (tokens[1] && tokens[1].value == 'def') {
methodDefFound = true;
methodDefIndentation = tokens[0].value;
}

// Search for the first empty line before the 'def' of the current method
if (methodDefFound && tokens[0] && tokens[0].value == '') {
this.extractMethodExecute(editor, cursor, rowNumber, methodDefIndentation);
break;
}
}
}
this.rowCountBetweenExtractedCodeAndDef = 0;
},

extractMethodExecute(editor, cursor, rowNumber, indentation) {
this.extractMethodWriteMethodAt(editor, rowNumber, indentation);
this.extractMethodCreateCursorOnNewMethod(editor);
this.extractMethodMoveCursorForRename(cursor);
},

extractMethodFetchBody(editor) {
editor.cutSelectedText();
atom.clipboard.read();
this.extractedMethod = atom.clipboard.read();
},

extractMethodWriteMethodAt(editor, position, indentation) {
var extractedMethodIndentationSpaces = Array(indentation.length + 1).join(' ');
var extractedMethodInnerIndentation = extractedMethodIndentationSpaces + Array(3).join(' ');
editor.insertText(extractedMethodInnerIndentation + '\n');

editor.setCursorBufferPosition([position, indentation.length]);
editor.insertText("\n" + extractedMethodIndentationSpaces + "def \n" + this.extractedMethod.replace(/\n$/, '') + "\n" + extractedMethodIndentationSpaces + "end\n");
this.extractedMethodPosition = position + 1;
},

extractMethodCreateCursorOnNewMethod(editor) {
var newMethodCursor = editor.addCursorAtBufferPosition([this.extractedMethodPosition, 0]);
newMethodCursor.moveToEndOfLine();
},

extractMethodMoveCursorForRename(cursor) {
cursor.setBufferPosition([cursor.getBufferRow() + this.rowCountBetweenExtractedCodeAndDef - 1, 0]);
cursor.moveToEndOfLine();
}
};
26 changes: 26 additions & 0 deletions menus/ror-refactor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"context-menu": {
"atom-text-editor": [
{
"label": "ROR Refactor: Extract Method",
"command": "ror-refactor:extract-method"
}
]
},
"menu": [
{
"label": "Packages",
"submenu": [
{
"label": "Ruby On Rails Refactor",
"submenu": [
{
"label": "Extract Method",
"command": "ror-refactor:extract-method"
}
]
}
]
}
]
}
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "ror-refactor",
"main": "./lib/ror-refactor",
"version": "0.1.0",
"description": "A set of refactoring tools for Ruby On Rails",
"keywords": [
"refactor",
"ruby",
"rails"
],
"activationCommands": {
"atom-text-editor": [
"ror-refactor:extract-method"
]
},
"repository": "https://github.com/zedtux/ror-refactor",
"license": "MIT",
"engines": {
"atom": ">=1.0.0 <2.0.0"
},
"dependencies": {
}
}
73 changes: 73 additions & 0 deletions spec/ror-refactor-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use babel';

import RorRefactor from '../lib/ror-refactor';

// Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs.
//
// To run a specific `it` or `describe` block add an `f` to the front (e.g. `fit`
// or `fdescribe`). Remove the `f` to unfocus the block.

describe('RorRefactor', () => {
let workspaceElement, activationPromise;

beforeEach(() => {
workspaceElement = atom.views.getView(atom.workspace);
activationPromise = atom.packages.activatePackage('ror-refactor');
});

describe('when the ror-refactor:toggle event is triggered', () => {
it('hides and shows the modal panel', () => {
// Before the activation event the view is not on the DOM, and no panel
// has been created
expect(workspaceElement.querySelector('.ror-refactor')).not.toExist();

// This is an activation event, triggering it will cause the package to be
// activated.
atom.commands.dispatch(workspaceElement, 'ror-refactor:toggle');

waitsForPromise(() => {
return activationPromise;
});

runs(() => {
expect(workspaceElement.querySelector('.ror-refactor')).toExist();

let rorRefactorElement = workspaceElement.querySelector('.ror-refactor');
expect(rorRefactorElement).toExist();

let rorRefactorPanel = atom.workspace.panelForItem(rorRefactorElement);
expect(rorRefactorPanel.isVisible()).toBe(true);
atom.commands.dispatch(workspaceElement, 'ror-refactor:toggle');
expect(rorRefactorPanel.isVisible()).toBe(false);
});
});

it('hides and shows the view', () => {
// This test shows you an integration test testing at the view level.

// Attaching the workspaceElement to the DOM is required to allow the
// `toBeVisible()` matchers to work. Anything testing visibility or focus
// requires that the workspaceElement is on the DOM. Tests that attach the
// workspaceElement to the DOM are generally slower than those off DOM.
jasmine.attachToDOM(workspaceElement);

expect(workspaceElement.querySelector('.ror-refactor')).not.toExist();

// This is an activation event, triggering it causes the package to be
// activated.
atom.commands.dispatch(workspaceElement, 'ror-refactor:toggle');

waitsForPromise(() => {
return activationPromise;
});

runs(() => {
// Now we can test for view visibility
let rorRefactorElement = workspaceElement.querySelector('.ror-refactor');
expect(rorRefactorElement).toBeVisible();
atom.commands.dispatch(workspaceElement, 'ror-refactor:toggle');
expect(rorRefactorElement).not.toBeVisible();
});
});
});
});
9 changes: 9 additions & 0 deletions spec/ror-refactor-view-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use babel';

import RorRefactorView from '../lib/ror-refactor-view';

describe('RorRefactorView', () => {
it('has one valid test', () => {
expect('life').toBe('easy');
});
});

0 comments on commit 9b525e9

Please sign in to comment.