highlight.js icon indicating copy to clipboard operation
highlight.js copied to clipboard

enh(swift) highlight function and macro call usage

Open bradleymackey opened this issue 1 year ago • 10 comments

Improve fidelity by highlighting function calls in addition to definitions. This is already done by javascript, for example.

Before After
Screenshot 2024-01-07 at 14 03 24 Screenshot 2024-01-07 at 14 05 49

Changes

  • Add negativeLookahead helper to lib/regex
  • Highlight all function and method calls as title.function (e.g. myFunction(), obj.myFunction()).
  • Highlight all freestanding macro calls as title.function (e.g. #myMacro()).
  • There can be >=0 whitespace between the end of the function name and the parentheses, but newlines are not permitted. Encapsulate this in a new regex, TRAILING_PAREN_REGEX
  • Built-ins still highlight as such but with exceptions for methods–where they are semantically no longer builtins.
  • Most keywords can exist as both properties and methods (e.g. obj.if()). Highlight keyword method calls also.
    • Number sign keywords and attribute keywords are permitted as raw functions, as they start with leading # and @ respectively. If they don't start with these, they're no longer considered reserved.
  • Highlight escaped function name calls also (e.g. `myFunc`()). This feature exists so that reserved words can be used as identifiers.
  • Variable accesses (e.g. obj.myVariable) remain unhighlighted for now.
  • Add markup tests for all the cases and edge cases described.

Next Steps (for a future PR)

  • Support trailing closures, which are method calls: exampleCall { print("action") }

Checklist

  • [x] Added markup tests
  • [x] Updated the changelog at CHANGES.md

bradleymackey avatar Jan 07 '24 14:01 bradleymackey

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

4 files changed

Total change +241 B

View Changes
file base pr diff
es/core.min.js 8.17 KB 8.17 KB +1 B
es/highlight.min.js 8.17 KB 8.17 KB +1 B
es/languages/swift.min.js 3.55 KB 3.67 KB +120 B
languages/swift.min.js 3.56 KB 3.68 KB +119 B

github-actions[bot] avatar Jan 07 '24 14:01 github-actions[bot]

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

3 files changed

Total change +231 B

View Changes
file base pr diff
es/languages/swift.min.js 3.55 KB 3.67 KB +116 B
highlight.min.js 8.21 KB 8.21 KB -1 B
languages/swift.min.js 3.56 KB 3.68 KB +116 B

github-actions[bot] avatar Jan 07 '24 14:01 github-actions[bot]

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +241 B

View Changes
file base pr diff
es/core.min.js 8.17 KB 8.17 KB +1 B
es/highlight.min.js 8.17 KB 8.17 KB +1 B
es/languages/swift.min.js 3.55 KB 3.67 KB +120 B
highlight.min.js 8.21 KB 8.21 KB -1 B
languages/swift.min.js 3.56 KB 3.68 KB +120 B

github-actions[bot] avatar Jan 07 '24 14:01 github-actions[bot]

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +207 B

View Changes
file base pr diff
es/core.min.js 8.17 KB 8.18 KB +2 B
es/highlight.min.js 8.17 KB 8.18 KB +2 B
es/languages/swift.min.js 3.55 KB 3.65 KB +101 B
highlight.min.js 8.21 KB 8.21 KB +2 B
languages/swift.min.js 3.56 KB 3.66 KB +100 B

github-actions[bot] avatar Jan 07 '24 14:01 github-actions[bot]

We can't use negative lookahead until v12 though because it's a breaking change. And I don't have a timetable on that yet.

joshgoebel avatar Jan 10 '24 02:01 joshgoebel

@joshgoebel This is an adapted version of Javascript's implementation which is already using negative lookahead ( see here). My understanding was that only lookbehind is not allowed currently.

bradleymackey avatar Jan 10 '24 07:01 bradleymackey

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +207 B

View Changes
file base pr diff
es/core.min.js 8.17 KB 8.17 KB +2 B
es/highlight.min.js 8.17 KB 8.17 KB +2 B
es/languages/swift.min.js 3.55 KB 3.65 KB +101 B
highlight.min.js 8.2 KB 8.21 KB +2 B
languages/swift.min.js 3.56 KB 3.66 KB +100 B

github-actions[bot] avatar Jan 13 '24 22:01 github-actions[bot]

@joshgoebel Is there a way forward for this PR? If the concern is a language-specific breaking change, the Swift language itself is already using negative lookahead as well, here.

bradleymackey avatar Jan 19 '24 11:01 bradleymackey

Sorry for delay. I think title.function might not be correct though typically we consider anything after . hanging off of an identifier (likely an object) a property... if the function is "global" or standalone then it could get function.title. IE:

debug_it("hello world")  # debug_it is `title.function` (seems to be global function call)
person.eat("sandwich") # `eat` is a property of person, that also appears to be a method/function

Thoughts?

joshgoebel avatar Jul 06 '24 22:07 joshgoebel