Git Product home page Git Product logo

swiftsyntaxhighlighter's Introduction

SwiftSyntaxHighlighter

CI Documentation

A syntax highlighter for Swift code that uses SwiftSyntax. You can use it either from the command-line, via the swift-highlight executable, or in Swift code using the SwiftSyntaxHighlighter module.

This functionality is discussed in the NSHipster article SwiftSyntax.

Requirements

  • Swift 5.3+

Command-Line Usage

The swift-highlight executable can be run from the command line to highlight either a path to a source file or source code:

$ swift highlight 'print("Hello, world!")'
<pre class="highlight"><code><span class="keyword">let</span> <span class="variable">greeting</span> = <span class="string literal">&quot;</span><span class="string literal">Hello, world!</span><span class="string literal">&quot;</span></code></pre>

Pass the --scheme pygments option to generate Pygments-compatible HTML:

$ swift highlight 'print("Hello, world!")' --scheme pygments
<pre class="highlight"><code><span class="n">print</span><span class="p">(</span><span class="p">&quot;</span><span class="s2">Hello, world!</span><span class="p">&quot;</span><span class="p">)</span></code></pre>

swift-highlight also accepts arguments piped from standard input (stdin):

echo 'print("Hello, world!")' | swift highlight
<pre class="highlight"><code><span class="variable">print</span>(<span class="string literal">&quot;</span><span class="string literal">Hello, world!</span><span class="string literal">&quot;</span>)
</code></pre>

Installation

Homebrew

Run the following command to install using homebrew:

$ brew install nshipster/formulae/swift-syntax-highlight

Manually

Run the following commands to build and install manually:

$ git clone https://github.com/NSHipster/SwiftSyntaxHighlighter.git
$ cd SwiftSyntaxHighlighter
$ make install

Code Usage

SwiftSyntaxHighlighter provides type methods named highlight that take either source code as a String, a source file URL, or a SourceFileSyntax AST created by SwiftSyntax.

import SwiftSyntaxHighlighter

let code = """
print("Hello, world!")
"""

let html = try SwiftSyntaxHighlighter.highlight(source: source, using: Xcode.self)

After running this code, html contains the following string:

<pre class="highlight"><code><span class="keyword">let</span> <span class="variable">greeting</span> = <span class="string literal">&quot;</span><span class="string literal">Hello, world!</span><span class="string literal">&quot;</span></code></pre>

Installation

Swift Package Manager

Add the SwiftSyntaxHighlighter package to your target dependencies in Package.swift:

import PackageDescription

let package = Package(
  name: "YourProject",
  dependencies: [
    .package(
        url: "https://github.com/NSHipster/SwiftSyntaxHighlighter",
        from: "1.2.4"
    ),
  ]
)

Then run the swift build command to build your project.

Known Issues

  • Xcode cannot run unit tests (โŒ˜U) when opening the SwiftSyntaxHighlighter package directly, as opposed first to generating an Xcode project file with swift package generate-xcodeproj. (The reported error is: Library not loaded: @rpath/lib_InternalSwiftSyntaxParser.dylib). As a workaround, you can install the latest toolchain and enable it in "Xcode > Preferences > Components > Toolchains". Alternatively, you can run unit tests from the command line with swift test.

License

MIT

Contact

Mattt (@mattt)

swiftsyntaxhighlighter's People

Contributors

belkadan avatar compnerd avatar fastestapp avatar franciscoamado avatar mattt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

swiftsyntaxhighlighter's Issues

dyld: Library not loaded: @rpath/lib_InternalSwiftSyntaxParser.dylib

Added SwiftSyntaxHighlighter using SPM in new macOS project and got error:

dyld: Library not loaded: @rpath/lib_InternalSwiftSyntaxParser.dylib
  Referenced from: {USER_FOLDER}/Library/Developer/Xcode/DerivedData/SwiftSyntaxMac-dcedkjjsloqegmatufrxjfbzzxub/Build/Products/Debug/SwiftSyntaxMac.app/Contents/MacOS/SwiftSyntaxMac
  Reason: image not found

Also found similar problems in other projects:
realm/SwiftLint#3105
uber/mockolo#108

Couldn't find solution.

Xcode Version 11.4.1 (11E503a)

Type detection has false positives and false negatives

case .identifier:
switch tokenSyntax.previousToken?.tokenKind {
case .classKeyword,
.enumKeyword,
.structKeyword,
.protocolKeyword,
.isKeyword,
.asKeyword,
.period,
.colon,
.leftSquareBracket where tokenSyntax.text == tokenSyntax.text.capitalized:
token = Token(tokenSyntax.text, kind: TypeName.self)
case .funcKeyword:
token = Token(tokenSyntax.text, kind: FunctionName.self)
case .atSign:
token = Token(tokenSyntax.text, kind: Attribute.self)
default:
token = Token(tokenSyntax.text, kind: Variable.self)
}

In this code, the where clause only applies to the .leftSquareBracket case, but not all (say) members are types. Beyond that, though, the capitalized property lowercases all further letters, meaning something like "UnsafePointer" wouldn't pass the test.

Installation on Big Sur via Homebrew failed

with the following error:

[8/9] Compiling SwiftSyntaxHighlighter SwiftSyntaxHighlighter.swift
[9/10] Compiling swift-highlight main.swift
ld: warning: dylib (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/lib_InternalSwiftSyntaxParser.dylib) was built for newer macOS version (10.12) than being linked (10.10)
[10/10] Linking swift-highlight
install: /private/tmp/swift-syntax-highlight-20201219-3997-1mtdrat/.build/release/swift-syntax-highlight: No such file or directory
make: *** [install] Error 71

I'm using Swift 5.3.2

Error installing with Carthage

I tried install SwiftSyntaxHighlighter by Carthage.
Here's my Cartfile:
github "NSHipster/SwiftSyntaxHighlighter" ~> 0.0.1

Then I run Carthage update and get this error(every times):

carthage-error

$ carthage update
*** Fetching SwiftSyntaxHighlighter
*** Checking out SwiftSyntaxHighlighter at "0.0.2"
*** xcodebuild output can be found in /var/folders/jf/kqw7xjn51m1b2jfg6956k3t00000gr/T/carthage-xcodebuild.hxbd2e.log
*** Skipped building SwiftSyntaxHighlighter due to the error:
Dependency "SwiftSyntaxHighlighter" has no shared framework schemes

If you believe this to be an error, please file an issue with the maintainers at https://github.com/NSHipster/SwiftSyntaxHighlighter/issues/new

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.