- Notifications
You must be signed in to change notification settings - Fork 136
Keep prev spaces #607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Keep prev spaces #607
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -367,7 +367,7 @@ def calc_indent_level(opens) | |
| indent_level = 0 | ||
| end | ||
| end | ||
| when :on_tstring_beg, :on_regexp_beg, :on_symbeg | ||
| when :on_tstring_beg, :on_regexp_beg, :on_symbeg, :on_backtick | ||
| # can be indented if t.tok starts with `%` | ||
| when :on_words_beg, :on_qwords_beg, :on_symbols_beg, :on_qsymbols_beg, :on_embexpr_beg | ||
| # can be indented but not indented in current implementation | ||
| | @@ -386,6 +386,26 @@ def free_indent_token?(token) | |
| FREE_INDENT_TOKENS.include?(token&.event) | ||
| end | ||
| | ||
| # Calculates the difference of pasted code's indent and indent calculated from tokens | ||
| def indent_difference(lines, line_results, line_index) | ||
| loop do | ||
| _tokens, prev_opens, _next_opens, min_depth = line_results[line_index] | ||
| open_token = prev_opens.last | ||
| if !open_token || (open_token.event != :on_heredoc_beg && !free_indent_token?(open_token)) | ||
| # If the leading whitespace is an indent, return the difference | ||
| indent_level = calc_indent_level(prev_opens.take(min_depth)) | ||
| calculated_indent = 2 * indent_level | ||
| actual_indent = lines[line_index][/^ */].size | ||
| return actual_indent - calculated_indent | ||
| elsif open_token.event == :on_heredoc_beg && open_token.tok.match?(/^<<[^-~]/) | ||
| return 0 | ||
| end | ||
| # If the leading whitespace is not an indent but part of a multiline token | ||
| # Calculate base_indent of the multiline token's beginning line | ||
| line_index = open_token.pos[0] - 1 | ||
| end | ||
| end | ||
| | ||
| def process_indent_level(tokens, lines, line_index, is_newline) | ||
| line_results = IRB::NestingParser.parse_by_line(tokens) | ||
| result = line_results[line_index] | ||
| | @@ -406,10 +426,20 @@ def process_indent_level(tokens, lines, line_index, is_newline) | |
| prev_open_token = prev_opens.last | ||
| next_open_token = next_opens.last | ||
| | ||
| # Calculates base indent for pasted code on the line where prev_open_token is located | ||
| # irb(main):001:1* if a # base_indent is 2, indent calculated from tokens is 0 | ||
| # irb(main):002:1* if b # base_indent is 6, indent calculated from tokens is 2 | ||
| # irb(main):003:0> c # base_indent is 6, indent calculated from tokens is 4 | ||
| if prev_open_token | ||
| base_indent = [0, indent_difference(lines, line_results, prev_open_token.pos[0] - 1)].max | ||
| Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this imply the result of Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. It could be negative. if 1 # diff = 0 if 2 # diff = 0 end end if 1 # diff = 4 if 2 # diff = 4 end end if 1 # diff = 4 if 2 # diff = 2 end endif 1 # diff = 0 if 2 # diff = -2 ← negative end end | ||
| else | ||
| base_indent = 0 | ||
| end | ||
| | ||
| if free_indent_token?(prev_open_token) | ||
| if is_newline && prev_open_token.pos[0] == line_index | ||
| # First newline inside free-indent token | ||
| indent | ||
| base_indent + indent | ||
| else | ||
| # Accept any number of indent inside free-indent token | ||
| preserve_indent | ||
| | @@ -427,21 +457,21 @@ def process_indent_level(tokens, lines, line_index, is_newline) | |
| if prev_opens.size <= next_opens.size | ||
| if is_newline && lines[line_index].empty? && line_results[line_index - 1][1].last != next_open_token | ||
| # First line in heredoc | ||
| indent | ||
| tok.match?(/^<<[-~]/) ? base_indent + indent : indent | ||
| elsif tok.match?(/^<<~/) | ||
| # Accept extra indent spaces inside `<<~` heredoc | ||
| [indent, preserve_indent].max | ||
| [base_indent + indent, preserve_indent].max | ||
| else | ||
| # Accept any number of indent inside other heredoc | ||
| preserve_indent | ||
| end | ||
| else | ||
| # Heredoc close | ||
| prev_line_indent_level = calc_indent_level(prev_opens) | ||
| tok.match?(/^<<[~-]/) ? 2 * (prev_line_indent_level - 1) : 0 | ||
| tok.match?(/^<<[~-]/) ? base_indent + 2 * (prev_line_indent_level - 1) : 0 | ||
| end | ||
| else | ||
| indent | ||
| base_indent + indent | ||
| end | ||
| end | ||
| | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to add
:on_backtickand the testtest_pasted_code_keep_base_indent_spacesfailed