[ruby/syntax_suggest] Support lexing with Prism
https://github.com/ruby/syntax_suggest/commit/7f4176a914
This commit is contained in:
parent
cce29750d7
commit
62c9695911
@ -5,23 +5,27 @@ require_relative "version"
|
|||||||
require "tmpdir"
|
require "tmpdir"
|
||||||
require "stringio"
|
require "stringio"
|
||||||
require "pathname"
|
require "pathname"
|
||||||
|
require "timeout"
|
||||||
|
|
||||||
# rubocop:disable Style/IdenticalConditionalBranches
|
# We need Ripper loaded for `Prism.lex_compat` even if we're using Prism
|
||||||
if ENV["SYNTAX_SUGGEST_DISABLE_PRISM"] # For testing dual ripper/prism support
|
# for lexing and parsing
|
||||||
require "ripper"
|
require "ripper"
|
||||||
|
|
||||||
|
# Prism is the new parser, replacing Ripper
|
||||||
|
#
|
||||||
|
# We need to "dual boot" both for now because syntax_suggest
|
||||||
|
# supports older rubies that do not ship with syntax suggest.
|
||||||
|
#
|
||||||
|
# We also need the ability to control loading of this library
|
||||||
|
# so we can test that both modes work correctly in CI.
|
||||||
|
if (value = ENV["SYNTAX_SUGGEST_DISABLE_PRISM"])
|
||||||
|
warn "Skipping loading prism due to SYNTAX_SUGGEST_DISABLE_PRISM=#{value}"
|
||||||
else
|
else
|
||||||
# TODO remove require
|
|
||||||
# Allow both to be loaded to enable more atomic commits
|
|
||||||
require "ripper"
|
|
||||||
begin
|
begin
|
||||||
require "prism"
|
require "prism"
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
require "ripper"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# rubocop:enable Style/IdenticalConditionalBranches
|
|
||||||
|
|
||||||
require "timeout"
|
|
||||||
|
|
||||||
module SyntaxSuggest
|
module SyntaxSuggest
|
||||||
# Used to indicate a default value that cannot
|
# Used to indicate a default value that cannot
|
||||||
|
@ -180,12 +180,19 @@ module SyntaxSuggest
|
|||||||
# EOM
|
# EOM
|
||||||
# expect(lines.first.trailing_slash?).to eq(true)
|
# expect(lines.first.trailing_slash?).to eq(true)
|
||||||
#
|
#
|
||||||
def trailing_slash?
|
if SyntaxSuggest.use_prism_parser?
|
||||||
last = @lex.last
|
def trailing_slash?
|
||||||
return false unless last
|
last = @lex.last
|
||||||
return false unless last.type == :on_sp
|
last&.type == :on_tstring_end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
def trailing_slash?
|
||||||
|
last = @lex.last
|
||||||
|
return false unless last
|
||||||
|
return false unless last.type == :on_sp
|
||||||
|
|
||||||
last.token == TRAILING_SLASH
|
last.token == TRAILING_SLASH
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Endless method detection
|
# Endless method detection
|
||||||
|
@ -32,18 +32,15 @@ module SyntaxSuggest
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
# rubocop:disable Style/IdenticalConditionalBranches
|
|
||||||
if SyntaxSuggest.use_prism_parser?
|
if SyntaxSuggest.use_prism_parser?
|
||||||
def self.lex(source, line_number)
|
def self.lex(source, line_number)
|
||||||
# Prism.lex_compat(source, line: line_number).value.sort_by {|values| values[0] }
|
Prism.lex_compat(source, line: line_number).value.sort_by { |values| values[0] }
|
||||||
Ripper::Lexer.new(source, "-", line_number).parse.sort_by(&:pos)
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
def self.lex(source, line_number)
|
def self.lex(source, line_number)
|
||||||
Ripper::Lexer.new(source, "-", line_number).parse.sort_by(&:pos)
|
Ripper::Lexer.new(source, "-", line_number).parse.sort_by(&:pos)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# rubocop:enable Style/IdenticalConditionalBranches
|
|
||||||
|
|
||||||
def to_a
|
def to_a
|
||||||
@lex
|
@lex
|
||||||
|
@ -8,6 +8,12 @@ end
|
|||||||
|
|
||||||
module SyntaxSuggest
|
module SyntaxSuggest
|
||||||
RSpec.describe "Top level SyntaxSuggest api" do
|
RSpec.describe "Top level SyntaxSuggest api" do
|
||||||
|
it "doesn't load prism if env var is set" do
|
||||||
|
skip("SYNTAX_SUGGEST_DISABLE_PRISM not set") unless ENV["SYNTAX_SUGGEST_DISABLE_PRISM"]
|
||||||
|
|
||||||
|
expect(SyntaxSuggest.use_prism_parser?).to be_falsey
|
||||||
|
end
|
||||||
|
|
||||||
it "has a `handle_error` interface" do
|
it "has a `handle_error` interface" do
|
||||||
fake_error = Object.new
|
fake_error = Object.new
|
||||||
def fake_error.message
|
def fake_error.message
|
||||||
|
Loading…
x
Reference in New Issue
Block a user