Граф коммитов

36 Коммитов

Автор SHA1 Сообщение Дата
Hiroshi SHIBATA 50b783ccac [ruby/syntax_suggest] Typofix by misspell
https://github.com/ruby/syntax_suggest/commit/66e1cf0b3e
2023-12-26 02:10:25 +00:00
Schneems 82883dc8f2 [ruby/syntax_suggest] Change assertion to not rely on exact text from prism
The original ripper test was very stable as the output didn't change. Prism is under active development and changing their output shouldn't cause a failure to the ruby/ruby test suite like https://github.com/ruby/ruby/actions/runs/7104601478/job/19339940315.

This commit moves from checking exact output to asserting that the string we get back is not empty. This should give the same level of confidence that some error message was caught, and is less brittle.

https://github.com/ruby/syntax_suggest/commit/4b6abb763e
2023-12-05 18:25:42 +00:00
Schneems 6d39d6d214 [ruby/syntax_suggest] Update docs, clean up PR
Removes or updates mentions of Ripper

https://github.com/ruby/syntax_suggest/commit/08aaa3f50a
2023-12-05 17:51:29 +00:00
Schneems 62c9695911 [ruby/syntax_suggest] Support lexing with Prism
https://github.com/ruby/syntax_suggest/commit/7f4176a914
2023-12-05 17:51:28 +00:00
Schneems cce29750d7 [ruby/syntax_suggest] Initial support for the prism parser
Prism will be the parser in Ruby 3.3. We need to support 3.0+ so we will have to "dual boot" both parsers.

Todo:

- LexAll to support Prism lex output
- Add tests that exercise both Ripper and prism codepaths on CI
- Handle https://github.com/ruby/prism/issues/1972 in `ripper_errors.rb`
- Update docs to not mention Ripper explicitly
- Consider different/cleaner APIs for separating out Ripper and Prism

https://github.com/ruby/syntax_suggest/commit/a7d6991cc4
2023-12-05 17:51:27 +00:00
Schneems 5a2d70ef73 [ruby/syntax_suggest] Bump minimum Ruby version & update standardrb
https://github.com/ruby/syntax_suggest/commit/73753518e9
2023-12-04 22:29:44 +00:00
Schneems 13482ab1e6 [ruby/syntax_suggest] Update standardrb to Ruby 3.0 standards
https://github.com/ruby/syntax_suggest/commit/2771dcabe0
2023-12-04 22:18:40 +00:00
Jean Boussier 460c27dc15 [ruby/syntax_suggest] Handle new eval source location
See https://bugs.ruby-lang.org/issues/19755

In Ruby 3.3, using `eval` without providing a source location
will now default to `"(eval at #{__FILE__}:#{__LINE__})"`.

https://github.com/ruby/syntax_suggest/commit/8e5076472e
2023-07-20 17:58:52 +00:00
Hiroshi SHIBATA a7d7032100
Manually merge syntax_suggest-1.1.0 2023-05-23 10:05:47 +09:00
schneems c638ffa700
[ruby/syntax_suggest] Fix
https://github.com/ruby/syntax_suggest/pull/187 Handle if/else with
empty/comment
line

Reported in #187 this code:

```
class Foo
  def foo
    if cond?
      foo
    else
      # comment
    end
  end

  # ...

  def bar
    if @recv
    end_is_missing_here
  end
end
```

Triggers an incorrect suggestion:

```
Unmatched keyword, missing `end' ?
   1  class Foo
   2    def foo
>  3      if cond?
>  5      else
   8    end
  16  end
```

Part of the issue is that while scanning we're using newlines to determine when to stop and pause. This is useful for determining logically smaller chunks to evaluate but in this case it causes us to pause before grabbing the "end" that is right below the newline. This problem is similar to https://github.com/ruby/syntax_suggest/pull/179.

However in the case of expanding same indentation "neighbors" I want to always grab all empty values at the end of the scan. I don't want to do that when changing indentation levels as it affects scan results.

There may be some way to normalize this behavior between the two, but the tests really don't like that change.

To fix this issue for expanding against different indentation I needed a way to first try and grab any additional newlines with the ability to rollback that guess. In #192 I experimented with decoupling scanning from the AroundBlockScan logic. It also added the ability to take a snapshot of the current scanner and rollback to prior changes.

With this ability in place now we:

- Grab extra empties before looking at the above/below line for the matching keyword/end statement
- If there's a match, grab it
- If there's no match, discard the newlines we picked up in the evaluation

That solves the issue.

https://github.com/ruby/syntax_suggest/commit/513646b912
2023-05-23 10:05:47 +09:00
schneems aaf815c626
[ruby/syntax_suggest] Refactor Scanner logic out of AroundBlockScan introduce history
AroundBlockScan started as a utility class that was meant to be used as a DSL for scanning and making new blocks. As logic got added to this class it became hard to reason about what exactly is being mutated when. I pulled the scanning logic out into it's own class which gives us a clean separation of concerns. This allowed me to remove a lot of accessors that aren't core to the logic provided by AroundBlockScan.

In addition to this refactor the ScanHistory class can snapshot a scan. This allows us to be more aggressive with scans in the future as we can now snapshot and rollback if it didn't turn out the way we wanted.

The change comes with a minor perf impact:

before: 5.092678   0.104299   5.196977 (  5.226494)
after: 5.128536   0.099871   5.228407 (  5.249542)

This represents a 0.996x change in speed (where 1x would be no change and 2x would be twice as fast). This is a 0.38% decrease in performance which is negligible. It's likely coming from the extra blocks being created while scanning. This is negligible and if the history feature works well we might be able to make better block decisions which is means fewer calls to ripper which is the biggest bottleneck.

While this doesn't fix https://github.com/ruby/syntax_suggest/issues/187 it's a good intermediate step that will hopefully make working on that issue easier.

https://github.com/ruby/syntax_suggest/commit/ad8487d8aa
2023-05-23 10:05:47 +09:00
schneems 3d5febf65b [ruby/syntax_suggest] Clean up output
I previously left a comment stating I didn't know why a certain method existed. In investigating the code in `CaptureCodeContext#capture_before_after_kws` I found that it was added as to give a slightly less noisy output.

The docs for AroundBlockScan#capture_neighbor_context only describe keywords as being a primary concern. I modified that code to only include lines that are keywords or ends. This reduces the output noise even more.

This allows me to remove that `start_at_next_line` method.

One weird side effect of the prior logic is it would cause this code to produce this output:

```
        class OH
          def hello

          def hai
          end
        end
```

```
          1  class OH
        > 2    def hello
          4    def hai
          5    end
          6  end
```

But this code to produce this output:

```
        class OH
          def hello
          def hai
          end
        end
```

```
          1  class OH
        > 2    def hello
          4    end
          5  end
```
Note the missing `def hai`. The only difference between them is that space.

With this change, they're now both consistent.

https://github.com/ruby/syntax_suggest/commit/4a54767a3e
2023-04-25 14:43:06 +09:00
Hiroshi SHIBATA f77dc6fb16 [ruby/syntax_suggest] standardrb --fix-unsafely spec/spec_helper.rb
https://github.com/ruby/syntax_suggest/commit/6e266b3b2b
2023-04-25 14:43:05 +09:00
Hiroshi SHIBATA 6ca1f3eec4
Load only SyntaxSuggest::VERSION for version check 2023-04-06 16:15:41 +09:00
schneems 7ab640d9dd
v1.0.4 2023-04-06 15:49:26 +09:00
schneems 8d72d6159c
v1.0.3
Fix a CI error and add a test to ensure we're testing the current version:

```
Run bundle exec rake test
bundler: failed to load command: rake (/home/runner/work/syntax_suggest/syntax_suggest/vendor/bundle/ruby/3.2.0/bin/rake)
/opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/runtime.rb:309:in `check_for_activated_spec!': You have already activated syntax_suggest 1.0.2, but your Gemfile requires syntax_suggest 1.0.3. Since syntax_suggest is a default gem, you can either remove your dependency on it or try updating to a newer version of bundler that supports syntax_suggest as a default gem. (Gem::LoadError)
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/runtime.rb:25:in `block in setup'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/spec_set.rb:138:in `each'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/spec_set.rb:138:in `each'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/runtime.rb:24:in `map'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/runtime.rb:24:in `setup'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler.rb:151:in `setup'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/setup.rb:20:in `block in <top (required)>'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/ui/shell.rb:136:in `with_level'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/ui/shell.rb:88:in `silence'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/setup.rb:20:in `<top (required)>'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/cli/exec.rb:56:in `require_relative'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/cli/exec.rb:56:in `kernel_load'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/cli/exec.rb:23:in `run'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/cli.rb:483:in `exec'
	from /opt/hostedtoolcache/Ruby/3.2.1/x64/lib/ruby/gems/3.2.0/gems/bundler-2.3.14/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
```
2023-04-06 15:47:04 +09:00
schneems 63ea6b0cf2 [ruby/syntax_suggest] Rollback comment indentation behavior
Originally I fixed https://github.com/ruby/syntax_suggest/pull/177 by making the process of comment removal indentation aware. The next commit is the more general fix and means we don't need to carry that additional logic/overhead.

Also: Update syntax via linter
2023-04-06 15:45:29 +09:00
schneems 2acbcec056 [ruby/syntax_suggest] Add comments and refactor AroundBlockScan methods
https://github.com/ruby/syntax_suggest/commit/cecd12292c
2023-04-06 15:45:29 +09:00
schneems 5487ee4fe8 [ruby/syntax_suggest] Fix sibling bug to #177
While #177 is reported as being caused by a comment, the underlying behavior is a problem due to the newline that we generated (from a comment). The prior commit fixed that problem by preserving whitespace before the comment. That guarantees that a block will form there from the frontier before it will be expanded there via a "neighbors" method. Since empty lines are valid ruby code, it will be hidden and be safe.

## Problem setup

This failure mode is not fixed by the prior commit, because the indentation is 0. To provide good results, we must make the algorithm less greedy. One heuristic/signal to follow is developer added newlines. If a developer puts a newline between code, it's more likely they're unrelated. For example:

```
port = rand(1000...9999)
stub_request(:any, "localhost:#{port}")

query = Cutlass::FunctionQuery.new(
  port: port
).call

expect(WebMock).to have_requested(:post, "localhost:#{port}").
  with(body: "{}")
```

This code is split into three chunks by the developer. Each are likely (but not guaranteed) to be intended to stand on their own (in terms of syntax). This behavior is good for scanning neighbors (same indent or higher) within a method, but bad for parsing neighbors across methods.

## Problem

Code is expanded to capture all neighbors, and then it decreases indent level which allows it to capture surrounding scope (think moving from within the method to also capturing the `def/end` definition. Once the indentation level has been increased, we go back to scanning neighbors, but now neighbors also contain keywords.

For example:

```
  1 def bark
  2
  3 end
  4
  5 def sit
  6 end
```

In this case if lines 4, 5, and 6 are in a block when it tries to expand neighbors it will expand up. If it stops after line 2 or 3 it may cause problems since there's a valid kw/end pair, but the block will be checked without it.

TLDR; It's good to stop scanning code after hitting a newline when you're in a method...it causes a problem scanning code between methods when everything inside of one of the methods is an empty line.

In this case it grabs the end on line 3 and since the problem was an extra end, the program now compiles correctly. It incorrectly assumes that the block it captured was causing the problem.

## Extra bit of context

One other technical detail is that after we've decided to stop scanning code for a new neighbor block expansion, we look around the block and grab any empty newlines. Basically adding empty newlines before of after a code block do not affect the parsing of that block.

## The fix

Since we know that this problem only happens when there's a newline inside of a method and we know this particular failure mode is due to having an invalid block (capturing an extra end, but not it's keyword) we have all the metadata we need to detect this scenario and correct it.

We know that the next line above our block must be code or empty (since we grabbed extra newlines). Same for code below it. We can count all the keywords and ends in the block. If they are balanced, it's likely (but not guaranteed) we formed the block correctly. If they're imbalanced, look above or below (depending on the nature of the imbalance), check to see if adding that line would balance the count.

This concept of balance and "leaning" comes from work in https://github.com/ruby/syntax_suggest/pull/152 and has proven useful, but not been formally introduced into the main branch.

## Outcome

Adding this extra check introduced no regressions and fixed the test case. It might be possible there's a mirror or similar problem that we're not handling. That will come out in time. It might also be possible that this causes a worse case in some code not under test. That too would come out in time.

One other possible concern to adding logic in this area (which is a hot codepath), is performance. This extra count check will be performed for every block. In general the two most helpful performance strategies I've found are reducing total number of blocks (therefore reducing overall N internal iterations) and making better matches (the parser to determine if a close block is valid or not is a major bottleneck. If we can split valid code into valid blocks, then it's only evaluated by the parser once, where as invalid code must be continuously re-checked by the parser until it becomes valid, or is determined to be the cause of the core problem.

This extra logic should very rarely result in a change, but when it does it should tend to produce slightly larger blocks (by one line) and more accurate blocks.

Informally it seems to have no impact on performance:

``
This branch:
DEBUG_DISPLAY=1 bundle exec rspec spec/ --format=failures  3.01s user 1.62s system 113% cpu 4.076 total
```

```
On main:
DEBUG_DISPLAY=1 bundle exec rspec spec/ --format=failures  3.02s user 1.64s system 113% cpu 4.098 total
```

https://github.com/ruby/syntax_suggest/commit/13739c6946
2023-04-06 15:45:28 +09:00
schneems e5236471c3 [ruby/syntax_suggest] Preserve whitespace in front of comments
When removing comments I previously replaced them with a newline. This loses some context and may affect the order of the indent search which in turn affects the final result. By preserving whitespace in front of the comment, we preserve the "natural" indentation order of the line while also allowing the parser/lexer to see and join naturally consecutive (method chain) lines.

close https://github.com/ruby/syntax_suggest/pull/177
2023-04-06 15:45:28 +09:00
Nobuyoshi Nakada dc1e6573f2
Fix commit miss 2023-03-09 00:49:11 +09:00
Nobuyoshi Nakada 611a64250a
Disable color mode of test-syntax-suggest when on dumb terminal
The compliation-mode of Emacs sets TERM to "dumb" and does not support
coloring.
2023-03-08 22:57:06 +09:00
Nobuyoshi Nakada e537aa65c0 [ruby/syntax_suggest] Run with the given ruby command
Running the file with shebang has a few issues.

* shebang is an OS dependent feature. Many modern UNIX-like OSes
  support it, but not all, e.g., Windows.
* `env` command may not be in `/usr/bin`.
* "ruby" command may not be "ruby", when `--program-suffix` or other
  configuration option is used.

https://github.com/ruby/syntax_suggest/commit/2edf241055
2023-01-07 17:02:49 +09:00
Hiroshi SHIBATA 4aeea3cc09 Removed the needless test guard for syntax_suggest.
It's resolved by 5bb43aeb89
2022-12-23 14:07:02 +09:00
Nobuyoshi Nakada bf3b376522 [ruby/syntax_suggest] Remove debug print
https://github.com/ruby/syntax_suggest/commit/4d53d31bc5
2022-12-23 13:05:26 +09:00
Nobuyoshi Nakada 5bb43aeb89 [ruby/syntax_suggest] Should not hardcode ruby name
https://github.com/ruby/syntax_suggest/commit/0d5201b24d
2022-12-23 13:05:25 +09:00
Hiroshi SHIBATA 93ac1503be Added condition for ruby/ruby repository 2022-12-09 16:36:22 +09:00
Hiroshi SHIBATA 0677bbe3ff Merge syntax_suggest master
Pick from daee74dcb0
2022-12-09 16:36:22 +09:00
Hiroshi SHIBATA d55d1a737b
Skip examples for SyntaxError extensions on Ruby 3.2 2022-12-02 10:34:00 +09:00
schneems d6e91784ab Update SyntaxSuggest to use angle brackets `>`
```
$ ruby tool/sync_default_gems.rb syntax_suggest
Sync ruby/syntax_suggest
```

https://github.com/ruby/syntax_suggest/pull/161
2022-11-30 12:58:47 +09:00
schneems f64ba0fadd
[ruby/syntax_suggest] Do not output "Syntax OK" when there's an error
Due to a problem with ripper we do not recognize `break` as invalid code. It's confusing that "Syntax OK" is output in that case.

When there's no syntax error, the algorithm should not say anything. The exception is in the CLI and that's for compatibility with `ruby -wc`

```
$ cat /tmp/break.rb
break
️ 3.1.2 🚀 /Users/rschneeman/Documents/projects/syntax_suggest (schneems/no-syntax-not-okay-break)
$ ruby -wc /tmp/break.rb
Syntax OK
```

> Note that this is invalid, running this code will raise a Syntax error.

```
$ exe/syntax_suggest /tmp/break.rb
Syntax OK
```

Close https://github.com/ruby/syntax_suggest/pull/157

https://github.com/ruby/syntax_suggest/commit/d7bd8f03a2
2022-11-28 20:55:41 +09:00
schneems 4d51a0b495
[ruby/syntax_suggest] Failing test for #157
https://github.com/ruby/syntax_suggest/commit/14e8cdc916
2022-11-28 20:55:41 +09:00
Hiroshi SHIBATA 5e4d1f9908 Skip examples to need installed ruby exe 2022-08-26 12:15:47 +09:00
Hiroshi SHIBATA 8dfc077f70 Added syntax_suggest cli and resolve failing exapmle with it 2022-08-26 12:15:47 +09:00
Hiroshi SHIBATA bd1b1eeb0e ruby-prof is now optional 2022-08-26 12:15:47 +09:00
Hiroshi SHIBATA 0d9f4ea0d4 Import spec examples from ruby/syntax_suggest 2022-08-26 12:15:47 +09:00