From eaba21d81ae4821790a1b7d1c70c4a9081db5585 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Tue, 5 Jan 2021 15:14:16 +0100 Subject: [PATCH] fmt: keep the shebang header in vsh files (#7893) --- vlib/v/fmt/fmt.v | 4 ++++ vlib/v/fmt/tests/vscript_keep.vv | 4 ++++ vlib/v/scanner/scanner.v | 4 ++-- 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 vlib/v/fmt/tests/vscript_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 6511669560..557344f889 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1355,6 +1355,10 @@ struct CommentsOptions { } pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { + if node.text.starts_with('#!') { + f.writeln(node.text) + return + } if options.iembed { x := node.text.trim_left('\x01') if x.contains('\n') { diff --git a/vlib/v/fmt/tests/vscript_keep.vv b/vlib/v/fmt/tests/vscript_keep.vv new file mode 100644 index 0000000000..372986bc5b --- /dev/null +++ b/vlib/v/fmt/tests/vscript_keep.vv @@ -0,0 +1,4 @@ +#!/usr/local/bin/v run + +x := 5 +println(x) diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 8e532a0c0a..6824d1889e 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -820,9 +820,9 @@ fn (mut s Scanner) text_scan() token.Token { s.ignore_line() if nextc == `!` { // treat shebang line (#!) as a comment - s.line_comment = s.text[start + 1..s.pos].trim_space() + comment := s.text[start - 1..s.pos].trim_space() // s.fgenln('// shebang line "$s.line_comment"') - continue + return s.new_token(.comment, comment, comment.len + 2) } hash := s.text[start..s.pos].trim_space() return s.new_token(.hash, hash, hash.len)