diff --git a/lib/prettier.rb b/lib/prettier.rb index 47b36b2d..7dfe92b4 100644 --- a/lib/prettier.rb +++ b/lib/prettier.rb @@ -3,6 +3,14 @@ require 'json' unless defined?(JSON) require 'open3' +class String + def match?(pattern) + pattern = Regexp.new(Regexp.escape(pattern)) unless pattern.is_a?(Regexp) + !!(self =~ pattern) + end +end + + module Prettier PLUGIN = -File.expand_path('..', __dir__) BINARY = -File.join(PLUGIN, 'node_modules', 'prettier', 'bin-prettier.js') diff --git a/src/haml/parser.rb b/src/haml/parser.rb index 84d84e3c..af7f8c1e 100644 --- a/src/haml/parser.rb +++ b/src/haml/parser.rb @@ -2,6 +2,14 @@ require 'ripper' +class String + def match?(pattern) + pattern = Regexp.new(Regexp.escape(pattern)) unless pattern.is_a?(Regexp) + !!(self =~ pattern) + end +end + + begin require 'haml' rescue LoadError diff --git a/src/parser/server.rb b/src/parser/server.rb index 39c06f7c..5a95564f 100644 --- a/src/parser/server.rb +++ b/src/parser/server.rb @@ -68,62 +68,68 @@ module Prettier break if quit # Start up a new thread that will handle each successive connection. - Thread.new(server.accept_nonblock) do |socket| - parser, source = socket.read.force_encoding('UTF-8').split('|', 2) - - response = - case parser - when 'ping' - 'pong' - when 'ruby' - SyntaxTree.parse(source) - when 'rbs' - Prettier::RBSParser.parse(source) - when 'haml' - Prettier::HAMLParser.parse(source) + begin + Thread.new(server.accept_nonblock) do |socket| + begin + parser, source = socket.read.force_encoding('UTF-8').split('|', 2) + + response = + case parser + when 'ping' + 'pong' + when 'ruby' + SyntaxTree.parse(source) + when 'rbs' + Prettier::RBSParser.parse(source) + when 'haml' + Prettier::HAMLParser.parse(source) + end + + if response + socket.write(JSON.fast_generate(response)) + else + socket.write('{ "error": true }') + end + rescue SyntaxTree::ParseError => error + loc = { start: { line: error.lineno, column: error.column } } + socket.write(JSON.fast_generate(error: error.message, loc: loc)) + rescue StandardError => error + begin + socket.write(JSON.fast_generate(error: error.message)) + rescue Errno::EPIPE + # Do nothing, the pipe has been closed by the parent process so we don't + # actually care about writing to it anymore. + end + ensure + socket.close end - - if response - socket.write(JSON.fast_generate(response)) - else - socket.write('{ "error": true }') - end - rescue SyntaxTree::ParseError => error - loc = { start: { line: error.lineno, column: error.column } } - socket.write(JSON.fast_generate(error: error.message, loc: loc)) - rescue StandardError => error - begin - socket.write(JSON.fast_generate(error: error.message)) - rescue Errno::EPIPE - # Do nothing, the pipe has been closed by the parent process so we don't - # actually care about writing to it anymore. end - ensure - socket.close - end - rescue IO::WaitReadable, Errno::EINTR - # Wait for select(2) to give us a connection that has content for 1 - # second. Otherwise timeout and continue on (so that we hit our - # "break if quit" pretty often). - IO.select([server], nil, nil, 1) + rescue IO::WaitReadable, Errno::EINTR + # Wait for select(2) to give us a connection that has content for 1 + # second. Otherwise timeout and continue on (so that we hit our + # "break if quit" pretty often). + IO.select([server], nil, nil, 1) - retry unless quit + retry unless quit + end end end # Map each candidate connection method to a thread that will check if it works. candidates.map! do |candidate| Thread.new do - Thread.current.report_on_exception = false + begin + Thread.current.report_on_exception = false - # We do not care about stderr here, so throw it away - stdout, _stderr, status = - Open3.capture3("#{candidate} #{information}", stdin_data: 'ping') + # We do not care about stderr here, so throw it away + stdout, _stderr, status = + Open3.capture3("#{candidate} #{information}", stdin_data: 'ping') - candidate if JSON.parse(stdout) == 'pong' && status.exitstatus == 0 - rescue StandardError - # We don't actually care if this fails, because we'll just skip that - # connection option. + candidate if JSON.parse(stdout) == 'pong' && status.exitstatus == 0 + rescue StandardError + # We don't actually care if this fails, because we'll just skip that + # connection option. + end end end diff --git a/src/rbs/parser.rb b/src/rbs/parser.rb index 39e15075..2bc12ce2 100644 --- a/src/rbs/parser.rb +++ b/src/rbs/parser.rb @@ -15,10 +15,18 @@ def self.parse(text) false end end +end + - return +class String + def match?(pattern) + pattern = Regexp.new(Regexp.escape(pattern)) unless pattern.is_a?(Regexp) + !!(self =~ pattern) + end end + + # This was removed at some point, so we need to support both versions. RBS::KEYWORD_RAW = if RBS::Parser.const_defined?(:KEYWORDS_RE)