diff options
author | Fawzi Mohamed <fawzi.mohamed@qt.io> | 2021-02-03 09:06:09 +0100 |
---|---|---|
committer | Fawzi Mohamed <fawzi.mohamed@qt.io> | 2021-02-15 18:22:27 +0100 |
commit | 6d51f997df14f2b22c265c5b2fda679ece9edef3 (patch) | |
tree | 551889cb0b96998d87c234c6cce657242c4ce954 /tools/qmlformat | |
parent | a8685fdb4d57c0ba36d80c395c2ae878595f04da (diff) |
Uniformly support shebang
The "qml" tool was the only way of loading QML files that would respect
a shebang line. This is problematic as this way you cannot load such
files programatically using QQmlComponent, limiting their re-use. Common
tools like Qt Creator, but also qmllint, qmlformat, qmlcachegen, etc
would not recognize files with shebangs.
By moving she-bang support directly in the lexer all tools implicitly
support it.
Note that we could just as easily support '#' as extra comment
character along with //, but here we narrowly add support for in
the first line only, as node does (this means that javascript files
using she-bang accepted by node, are now accepted also by qml).
The only tool needing some adjustments is qmlformat, that has to emit
the she-bang again as she-bang and as first line.
Add tests for qmlformat, and sprinkle some she-bangs in the other
tests just to be sure it doesn't affect anything.
Change-Id: I1f6d881c7438bdb23163b5dbe829d59a35d11132
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'tools/qmlformat')
-rw-r--r-- | tools/qmlformat/commentastvisitor.h | 1 | ||||
-rw-r--r-- | tools/qmlformat/dumpastvisitor.cpp | 2 |
2 files changed, 2 insertions, 1 deletions
diff --git a/tools/qmlformat/commentastvisitor.h b/tools/qmlformat/commentastvisitor.h index 6ebf8246ba..09bc786985 100644 --- a/tools/qmlformat/commentastvisitor.h +++ b/tools/qmlformat/commentastvisitor.h @@ -65,6 +65,7 @@ struct Comment QList<SourceLocation> m_srcLocations; + bool hasSheBang() const { return !m_srcLocations.isEmpty() && m_srcLocations.first().begin() == 0; } bool isValid() const { return !m_srcLocations.isEmpty(); } bool isMultiline() const { return m_text.contains("\n"); } bool isSyntheticMultiline() const { return m_srcLocations.size() > 1; } diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp index 771814ffd5..723be4e445 100644 --- a/tools/qmlformat/dumpastvisitor.cpp +++ b/tools/qmlformat/dumpastvisitor.cpp @@ -129,7 +129,7 @@ QString DumpAstVisitor::formatComment(const Comment &comment) const if (useMultilineComment) result += "/*"; - else + else if (!comment.hasSheBang()) result += "//"; result += comment.m_text; |