diff --git a/corner_cases.jl b/corner_cases.jl index 06b8b8d..34ec449 100644 --- a/corner_cases.jl +++ b/corner_cases.jl @@ -18,7 +18,7 @@ function foo_bar!(x,y) x + y + 1 end -# Expected: foo_bar!` should be highlighted. +# Expected: `foo_bar!` should be highlighted. foo_bar!(x,y) = x + y # Expected: `foo` should be highlighted. @@ -54,6 +54,9 @@ cell(dims::(Integer...)) = Array(Any, convert((Int...), dims)) # Expected: `@hello_world!` should be highlighted. @hello_world! foo +# Expected: highlight `myfun` +@inline myfun() = println("myfun") + ## Builtin functions. # Expected: `throw`, `error` and `super` should not be highlighted. There are # too many built-in functions for this to be useful. @@ -75,6 +78,7 @@ bar" x = """hello "world" foobar""" y = """foo\\""" z = """bar\"""" +w = """"bar""" # Expected: highlight `$user` x = "hello $user" @@ -82,6 +86,9 @@ x = "hello $user" # Expected: don't highlight `$user` x = "hello \$user" +# Expected: highlight `$val` +x = """(a="$val")""" + # Expected: treat r as part of the string, so `r"a"` is highlighted. x = r"0.1" @@ -109,20 +116,32 @@ x = r"[abc]" # Expected: highlight escape sequences `\xff` and `\u2200` x = b"DATA\xff\u2200" +# Bonus points: +# Expected: don't highlight `$user` +x = raw"hello $user" + ## Characters # Expected: highlight the character. x = 'a' y = '\u0' z = '\U10ffff' +w = '\x41' a = ' ' b = '"' +c = ''' +d = '\'' +e = '\\' # Expected: don't highlight, as ' is an operator here, not a character delimiter. a = b' + c' +A''' +x'x +x'x' # Bonus points: # Expected: don't highlight the character x = 'way too long so not a character' +x = '' ## Comments # Expected: highlight `# foo` @@ -135,6 +154,26 @@ bar =# # Expected: highlight `#= #= =# =#` (comments can nest). #= #= =# =# +# Expected: highlight all as nested comments. +#= + #= + #= + =# + =# +=# + + +# Expected: highlight `'` as adjoint operator +A#==#' +(A)#==#' +A[1]#==#' + +# Expected: highlight `'` as adjoint operator `#'` after as comment +table!'#' + +# Expected: highlight `'#'` as char +table[]!='#' + ## Type declarations # Expected highlight `Foo` and `Bar` @@ -148,7 +187,7 @@ struct Foo end # Expected: highlight `Foo` and `Bar` -abstract type Foo <: Bar +abstract type Foo <: Bar end # Expected: don't highlight x or y x <: y @@ -164,10 +203,32 @@ function foo() x end +# Expected: highlight `Point` and `Real` as types +function norm(p::Point{<:Real}) + sqrt(p.x^2 + p.y^2) +end + +# Expected: highlight `g` as function and `Int8` as type +function g(x, y)::Int8 + return x * y +end + # Expected: highlight `T` and `Number` same_type_numeric(x::T, y::T) where {T <: Number} = true +same_type_numeric(x::T, y::T) where T = false + +## Parametric type declaration -## Variable delcarations +# Expected: highlight `Pointy` and `T` +abstract type Pointy{T} end + +# Expected: highlight `Point`, `Pointy` and `T` +struct Point{T} <: Pointy{T} + x::T + y::T +end + +## Variable declarations # Expected: highlight `x` and `y` global x = "foo, bar = 2", y = 3 @@ -204,6 +265,9 @@ end # Expected: highlight as index `end` foo[bar:end] +# Expected: highlight as index `begin` +foo[begin:4] + # Expected: don't highlight `:123` x = :123 @@ -213,7 +277,7 @@ foo[bar:baz] # Expected: highlight `:baz` foo[:baz] -# Expected: highlight `:baz` +# Expected: highlight both `:baz` foo(:baz,:baz) # Note that `: foo` is currently a valid quoted symbol, this will hopefully @@ -225,18 +289,52 @@ foo(:baz,:baz) # Expected: highlight `:end` [1 :end] +# Expected: highlight `:two` +@eval :one+:two + +# Expected: don't highlight `:end` but `end` as index +[(1+1):end] + +# Expected: don't highlight `:end` but `end` as index +[a[1]:end] + # Expected: don't highlight `:foo` for x=1:foo print(x) end +## Range detection + +# Bonus points: +# Expected: don't highlight `:s2` +push!(a, s1 :s2) + +# Bonus points: +# Expected: don't highlight `:end` +a[begin :end] + +## Expression evaluation + +# Expected: highlight `:` as operator +a = :(x = 2) + +# Expected: highlight `:call` and `:b` as symbols +# Debatable: highlight `:+` as operator +ex = Expr(:call, :+, a, :b) + + +## Pairs and anonymous functions + +# Expected: highlight `:b` as a symbol and `->` and `=>` as operators +Dict(1=>:b) +x->:b + ## Number highlighting # Expected: highlight all these as numbers x = 123 x = 1.1 x = .5 -x = -1 x = 0x123abcdef x = 0o7 x = 0b1011 @@ -245,17 +343,92 @@ x = 2.5E-4 x = 1e+00 x = 2.5f-4 x = 0x.4p-1 +x = 1_000 # Expected: highlight these as numbers or built-ins x = Inf x = NaN -# Expected: highlight `123` +# Expected: highlight `123`, not the letter y = 123x +y = 123e + +# Expected: highlight `1e+1` and `1e-1` +1e+1+1e-1 + +# Expected: highlight `1.` and `.1` +1. +.1 +# Note that `1.+1` is currently ambiguous and gives an error + +# Bonus points: +# Expected: don't highlight `..` +x = 1..3 + +# Bonus points: +# Debatable: highlight the first two digits, not the following digits +# or show an error +x = 0o1291 +x = 0b1091 # Debatable: highlight `π` as a number or built-in # (note that `πx` is a single symbol, not `π * x`) x = π -# Note that `x.3` is currently equivalent to `.3x` or `.3 * x`, but this -# may change: https://github.com/JuliaLang/julia/issues/6770 +## List comprehension +# Expected: highlight `for` and `if` without the `end` keyword +[i for i in 1:5 if i%2==0] + +## Broadcasting +# Expected: highlight `.+` as operator +a.+1 + +## Command +# Expected: highlight "`echo 1`" +c = `echo 1` + +# Expected: highlight "```echo `hello 1` ```" +c = ```echo `hello 1` ``` + +# Expected: highlight "raw`echo $1`" +c = raw`echo $1` + +## Non-standard identifiers +# Bonus points: +# Expected: highlight `var"##"` as a function +function var"##"(x) + println(x) +end + +# Bonus points: +# Expected: highlight `var"%%"` as a function +var"%%"(x) = println(x) + +# Bonus points: +# Expected: highlight `$var` as string and `##""` as comment +"$var"##"" + +# Bonus points: +# Expected: highlight `$(var")(")` as string interpolation +"$(var")(")" + +# Bonus points: +# Expected: highlight `'` as adjoint operator +var"##mat"' + +## Code folding: for and if in list comprehension +# Expected: fold between function and last end +function test(x) + a = (if x; 0 else 1 end) + println(a) +end + +# Expected: no folding. `begin` and `end` are keywords, not indices, `for` and `if` do not need `end` keywords +b = [(begin n%2; n*2 end) for n in 1:10 if n>1] + +# Expected: no folding. `if`, `else` and `end` are keywords, not indices, `for` does not need an `end` keyword +c = [(if isempty(a); missing else first(b) end) for (a, b) in zip(l1, l2)] + +# Expected: no folding. `for` does not need an `end` keyword +d = (i for i in 1:10) + +## END diff --git a/test_file.jl b/test_file.jl index 6ca43ab..7751b50 100644 --- a/test_file.jl +++ b/test_file.jl @@ -1,6 +1,16 @@ -## Julia syntax highlighting test. -# This file is designed to test various corner cases of Julia -# syntax highlighting. +#= Julia syntax highlighting test. + +This file derives from https://gist.github.com/Wilfred/f1aca44c61ed6e1df603 +whose author is [@Wilfred](https://github.com/Wilfred). @Wilfred has put it in +the public domain: + https://gist.github.com/Wilfred/f1aca44c61ed6e1df603#gistcomment-2948526 + +Changes from the original are governed by the license of the repository in +which this file is found. + +This file is designed to test various corner cases of Julia +syntax highlighting. +=# ## Simple function definitions. # Expected: `function` should be highlighted, as should `foo_bar!`. @@ -28,9 +38,9 @@ end ## Function definitions with type variables. # Expected: `elsize` should be highlighted. -elsize{T}(::AbstractArray{T}) = sizeof(T) +elsize(::AbstractArray{T}) where {T} = sizeof(T) -function elsize{T}(::AbstractArray{T}) +function elsize(::AbstractArray{T}) where T sizeof(T) end @@ -128,17 +138,17 @@ bar =# ## Type declarations # Expected highlight `Foo` and `Bar` -type Foo +mutable struct Foo x::Bar end # Expected highlight `Foo` and `Bar` -immutable Foo +struct Foo x::Bar end # Expected: highlight `Foo` and `Bar` -abstract Foo <: Bar +abstract type Foo <: Bar end # Expected: don't highlight x or y x <: y @@ -155,7 +165,7 @@ function foo() end # Expected: highlight `T` and `Number` -same_type_numeric{T<:Number}(x::T, y::T) = true +same_type_numeric(x::T, y::T) where {T <: Number} = true ## Variable delcarations