diff --git a/core/alloc.py b/core/alloc.py index f8d3b7f33c..3858ebf166 100644 --- a/core/alloc.py +++ b/core/alloc.py @@ -119,21 +119,25 @@ def GetCodeString(self, lbrace_spid, rbrace_spid): right_id = right_span.line_id assert left_id <= right_id - left_col = left_span.col + left_col = left_span.col # 0-based indices right_col = right_span.col parts = [] + parts.append(' ' * (left_col+1)) # pad with spaces so column numbers are the same - # first incomplete line - parts.append(' ' * left_col) # pad with spaces so column numbers are the same - parts.append(self.line_vals[left_id][left_col+1:]) + if left_id == right_id: + # the single line + parts.append(self.line_vals[left_id][left_col+1:right_col]) + else: + # first incomplete line + parts.append(self.line_vals[left_id][left_col+1:]) - # all the complete lines - for line_id in xrange(left_id + 1, right_id): - parts.append(self.line_vals[line_id]) + # all the complete lines + for line_id in xrange(left_id + 1, right_id): + parts.append(self.line_vals[line_id]) - # last incomplete line - parts.append(self.line_vals[right_id][:right_col]) + # last incomplete line + parts.append(self.line_vals[right_id][:right_col]) return ''.join(parts) diff --git a/osh/builtin_pure.py b/osh/builtin_pure.py index f153da1029..41ae127e3f 100644 --- a/osh/builtin_pure.py +++ b/osh/builtin_pure.py @@ -813,8 +813,9 @@ def Run(self, cmd_val): src = self.arena.GetLineSource(line_span.line_id) line_num = self.arena.GetLineNumber(line_span.line_id) - result['location_file'] = ui.GetLineSourceString(self.arena, - line_span.line_id) + # for the user to pass back to --location-str + result['location_str'] = ui.GetLineSourceString(self.arena, + line_span.line_id) result['location_start_line'] = line_num # Between { and } diff --git a/spec/hay.test.sh b/spec/hay.test.sh index 3558291d22..d4dbd2705b 100644 --- a/spec/hay.test.sh +++ b/spec/hay.test.sh @@ -197,13 +197,38 @@ shopt --set parse_brace parse_equals hay define TASK hay eval :result { - TASK build { - echo hi + TASK { echo hi } + + TASK { + echo one + echo two } } #= result -json write (result) +json write (result) | jq . > out.txt + +diff -u - out.txt <<'EOF' +{ + "source": null, + "children": [ + { + "type": "TASK", + "args": [], + "location_str": "[ stdin ]", + "location_start_line": 6, + "code_str": " echo hi " + }, + { + "type": "TASK", + "args": [], + "location_str": "[ stdin ]", + "location_start_line": 8, + "code_str": " \n echo one\n echo two\n " + } + ] +} +EOF ## STDOUT: ## END diff --git a/test/spec.sh b/test/spec.sh index 10971bd0f9..9f70b75895 100755 --- a/test/spec.sh +++ b/test/spec.sh @@ -903,7 +903,7 @@ oil-blocks() { } hay() { - sh-spec spec/hay.test.sh --osh-failures-allowed 10 \ + sh-spec spec/hay.test.sh --osh-failures-allowed 9 \ $OSH_LIST "$@" }