Skip to content
This repository

Improved auto-indent and dedent for ruby, now with test coverage #1305

Merged
merged 2 commits into from over 1 year ago

2 participants

Chris Nelson Harutyun Amirjanyan
Chris Nelson

No description provided.

Harutyun Amirjanyan nightwing merged commit 312d36c into from
Harutyun Amirjanyan
Collaborator

Cool! merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 91 additions and 7 deletions. Show diff stats Hide diff stats

  1. +13 7 lib/ace/mode/ruby.js
  2. +78 0 lib/ace/mode/ruby_test.js
20 lib/ace/mode/ruby.js
@@ -3,7 +3,7 @@
3 3 *
4 4 * Copyright (c) 2010, Ajax.org B.V.
5 5 * All rights reserved.
6   - *
  6 + *
7 7 * Redistribution and use in source and binary forms, with or without
8 8 * modification, are permitted provided that the following conditions are met:
9 9 * * Redistributions of source code must retain the above copyright
@@ -14,7 +14,7 @@
14 14 * * Neither the name of Ajax.org B.V. nor the
15 15 * names of its contributors may be used to endorse or promote products
16 16 * derived from this software without specific prior written permission.
17   - *
  17 + *
18 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -48,7 +48,7 @@ oop.inherits(Mode, TextMode);
48 48
49 49 (function() {
50 50
51   -
  51 +
52 52 this.lineCommentStart = "#";
53 53
54 54 this.getNextLineIndent = function(state, line, tab) {
@@ -60,10 +60,13 @@ oop.inherits(Mode, TextMode);
60 60 if (tokens.length && tokens[tokens.length-1].type == "comment") {
61 61 return indent;
62 62 }
63   -
  63 +
64 64 if (state == "start") {
65 65 var match = line.match(/^.*[\{\(\[]\s*$/);
66   - if (match) {
  66 + var startingClassOrMethod = line.match(/^\s*(class|def)\s.*$/);
  67 + var startingDoBlock = line.match(/.*do(\s*|\s+\|.*\|\s*)$/);
  68 + var startingConditional = line.match(/^\s*(if|else)\s*/)
  69 + if (match || startingClassOrMethod || startingDoBlock || startingConditional) {
67 70 indent += tab;
68 71 }
69 72 }
@@ -72,11 +75,14 @@ oop.inherits(Mode, TextMode);
72 75 };
73 76
74 77 this.checkOutdent = function(state, line, input) {
75   - return this.$outdent.checkOutdent(line, input);
  78 + return /\s+end$/.test(line + input) || /\s+}$/.test(line + input) || /\s+else$/.test(line + input);
76 79 };
77 80
78 81 this.autoOutdent = function(state, doc, row) {
79   - this.$outdent.autoOutdent(doc, row);
  82 + var indent = this.$getIndent(doc.getLine(row));
  83 + var tab = doc.getTabString();
  84 + if (indent.slice(-tab.length) == tab)
  85 + doc.remove(new Range(row, indent.length-tab.length, row, indent.length));
80 86 };
81 87
82 88 }).call(Mode.prototype);
78 lib/ace/mode/ruby_test.js
... ... @@ -0,0 +1,78 @@
  1 +/* ***** BEGIN LICENSE BLOCK *****
  2 + * Distributed under the BSD license:
  3 + *
  4 + * Copyright (c) 2010, Ajax.org B.V.
  5 + * All rights reserved.
  6 + *
  7 + * Redistribution and use in source and binary forms, with or without
  8 + * modification, are permitted provided that the following conditions are met:
  9 + * * Redistributions of source code must retain the above copyright
  10 + * notice, this list of conditions and the following disclaimer.
  11 + * * Redistributions in binary form must reproduce the above copyright
  12 + * notice, this list of conditions and the following disclaimer in the
  13 + * documentation and/or other materials provided with the distribution.
  14 + * * Neither the name of Ajax.org B.V. nor the
  15 + * names of its contributors may be used to endorse or promote products
  16 + * derived from this software without specific prior written permission.
  17 + *
  18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21 + * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28 + *
  29 + * ***** END LICENSE BLOCK ***** */
  30 +
  31 +if (typeof process !== "undefined") {
  32 + require("amd-loader");
  33 +}
  34 +
  35 +define(function(require, exports, module) {
  36 +"use strict";
  37 +
  38 +var EditSession = require("../edit_session").EditSession;
  39 +var Tokenizer = require("../tokenizer").Tokenizer;
  40 +var Mode = require("./ruby").Mode;
  41 +var assert = require("../test/assertions");
  42 +
  43 +module.exports = {
  44 + setUp : function() {
  45 + this.mode = new Mode();
  46 + },
  47 +
  48 + "test getNextLineIndent": function() {
  49 + assert.equal(this.mode.getNextLineIndent("start", "class Foo", " "), " ");
  50 + assert.equal(this.mode.getNextLineIndent("start", " def thing(wut)", " "), " ");
  51 + assert.equal(this.mode.getNextLineIndent("start", " fork do", " "), " ");
  52 + assert.equal(this.mode.getNextLineIndent("start", " fork do |wut| ", " "), " ");
  53 + assert.equal(this.mode.getNextLineIndent("start", " something = :ruby", " "), " ");
  54 + assert.equal(this.mode.getNextLineIndent("start", " if something == 3", " "), " ");
  55 + assert.equal(this.mode.getNextLineIndent("start", " else", " "), " ");
  56 + },
  57 +
  58 + "test: checkOutdent": function() {
  59 + assert.ok(this.mode.checkOutdent("start", " en", "d"));
  60 + assert.ok(this.mode.checkOutdent("start", " els", "e"));
  61 + assert.ok(this.mode.checkOutdent("start", " ", "}"));
  62 + assert.equal(this.mode.checkOutdent("start", " end", "\n"), false);
  63 + assert.equal(this.mode.checkOutdent("start", "foo = ba", "r"), false);
  64 + },
  65 +
  66 + "test: auto outdent" : function() {
  67 + var session = new EditSession(["class Phil", " Foo = 'bar'", " end"]);
  68 + this.mode.autoOutdent("start", session, 2);
  69 + assert.equal(" end", session.getLine(2));
  70 + }
  71 +
  72 +};
  73 +
  74 +});
  75 +
  76 +if (typeof module !== "undefined" && module === require.main) {
  77 + require("asyncjs").test.testcase(module.exports).exec()
  78 +}

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.