@exercism/codemirror-lang-ruby
v1.0.2
Published
Ruby language support for the CodeMirror code editor, with completion, as used on Exercism
Readme
@exercism/codemirror-lang-ruby 
[ CHANGELOG ]
This package implements Ruby language support for the CodeMirror code editor, using a Lezer grammar written for this package. Exercism uses it in the Ruby editor on exercism.org.
This code is released under an MIT license.
Install
npm install @exercism/codemirror-lang-rubyUsage
import {EditorView, basicSetup} from "codemirror"
import {ruby} from "@exercism/codemirror-lang-ruby"
const view = new EditorView({
parent: document.body,
doc: `class Greeter
def initialize(name)
@name = name
end
def greet
puts "Hello, #{@name}!"
end
end
`,
extensions: [basicSetup, ruby()]
})Completion
ruby() includes completion for:
- Keywords, the methods called without a receiver (such as
putsandattr_reader), and the core classes. - Snippets for methods, classes, modules, conditionals, loops,
case,beginandrescue, blocks and lambdas. - The names declared in the file. A local variable or parameter is offered in
the scope that declares it, once it has been declared. Methods, classes and
constants are offered everywhere. After
@, the instance variables of the surrounding class are offered. - The common methods of the core classes, after a
..
Nothing is offered inside strings, comments or symbols. Code inside #{...}
is completed like any other code.
Coverage
Ruby 3.3. Pattern matching, endless methods, hash shorthand, safe navigation,
numbered block parameters, rational and imaginary literals, and everything
older that is still written every day: heredocs of all four kinds, percent
literals with any delimiter, singleton classes, operator methods, begin
blocks with their rescue and ensure, and blocks in both spellings.
The parser reads endless singleton methods (def self.limit = 50), and reads
n %= 60 as an assignment when that line comes after another line of code.
Measured against 20000 files of published gems and applications, 95.67% parse with no error node. What is left is flat: 1668 shapes over 865 files, and the largest of them covers a few dozen.
What a tokenizer has to settle
Almost all of Ruby can be written down as a grammar. Seven things cannot, and
they are the whole of tokens.ts.
A slash is a pattern or a division. a / b divides and a =~ /b/ matches,
and the same character opens both. Where a division cannot stand the parser has
already ruled it out; where both could, Ruby's own rule decides, which is that
a /b/ with a space in front and none behind is a call taking a pattern and
a / b is arithmetic.
A brace is a block or a hash. each { |x| } passes a block and {a: 1} is
a value. Ruby tells them apart by what stands in front of the brace, so the
tokenizer asks the parser whether a block could stand here and lets the answer
decide. The same question settles f(x) against (x), def f(x) against
both, and while x do against xs.each do.
A newline ends a statement. x = 1 followed by if y is two statements;
x = 1 if y is one. rescue splits the same way: on its own line it opens a
clause, and after an expression it catches one. So does the end of an argument
list written without parentheses. This grammar skips newlines like any other
space, so each of those is decided by looking at where the line began, and the
last of them is marked with a token of no width.
A heredoc's body is nowhere near its name. <<~SQL promises a body that
starts on the next line, and one line may promise several. Rather than carry
the promise from one token to the next, the body is read at the newline by
looking back over the line that just ended, and then skipped like whitespace.
A colon is a hash key or a punctuation mark. a: written against a name is
a key, and the second colon of A::B says it is not. A regular token takes the
longer match and reads A::B as the key A: followed by the symbol :B, so
the second colon is looked at before the first is believed.
A percent literal chooses its own delimiter. %w[a b], %i(x y), %q{...}
and %r|...| are one form with the delimiter picked per use, and the bracketing
ones nest.
A string holds code. "total: #{a + b}" is a string with an expression
inside it, and that expression may hold another string. The tokenizer cuts the
string into pieces and the grammar parses what lies between them.
What the tree does not say
Two things are deliberately shallower than the language reference.
A call without parentheses takes one argument. puts result and
Marshal.load(Marshal.dump orig) are read as calls; foo a, b without
brackets keeps only the first argument in the call. Ruby decides the rest by
knowing whether each name is a method or a local variable, which is a question
about the program rather than about the text.
Patterns are expressions. case x in {name: String} holds a shape that only
appears after in. Rather than carry a second grammar for patterns, the hash
and the array read as the literals they resemble, and only the pin (^x) and
the alternation are written down separately.
Building and testing
npm install # installs the dependencies and builds dist/
npm run build # regenerates the parser and rebuilds dist/
npm test # builds, then runs the testsContributing
Please read about how to contribute to Exercism. Be sure to read the Exercism Code of Conduct.
