From 1ee9e55fec96116825898c12ab07af39b9f4229f Mon Sep 17 00:00:00 2001 From: getzze Date: Wed, 28 Oct 2020 15:17:33 +0000 Subject: [PATCH 1/7] update syntax to julia-1.0 --- test_file.jl | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/test_file.jl b/test_file.jl index 6ca43ab..06b8b8d 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 # 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 From d9cc707b0fc38ae3059b01378effaa6356846444 Mon Sep 17 00:00:00 2001 From: getzze Date: Wed, 28 Oct 2020 15:17:59 +0000 Subject: [PATCH 2/7] additional corner cases --- corner_cases.jl | 154 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 146 insertions(+), 8 deletions(-) diff --git a/corner_cases.jl b/corner_cases.jl index 06b8b8d..6354d37 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,30 @@ 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''' # Bonus points: # Expected: don't highlight the character x = 'way too long so not a character' +x = '' ## Comments # Expected: highlight `# foo` @@ -135,6 +152,11 @@ bar =# # Expected: highlight `#= #= =# =#` (comments can nest). #= #= =# =# +# Expected: highlight `'` as adjoint operator +A#==#' +(A)#==#' +A[1]#==#' + ## Type declarations # Expected highlight `Foo` and `Bar` @@ -148,7 +170,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 +186,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 + +# 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 delcarations +## Variable declarations # Expected: highlight `x` and `y` global x = "foo, bar = 2", y = 3 @@ -204,6 +248,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 +260,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 +272,45 @@ 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) + ## 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 +319,81 @@ 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 From c3b099478d1ed53d1a6545bfb33fcfe03450fe45 Mon Sep 17 00:00:00 2001 From: getzze Date: Wed, 28 Oct 2020 15:26:55 +0000 Subject: [PATCH 3/7] correct abstract struct --- test_file.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_file.jl b/test_file.jl index 06b8b8d..7751b50 100644 --- a/test_file.jl +++ b/test_file.jl @@ -148,7 +148,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 From 2daf021f6cd9b90625964296e3c9d8fcdf5f3129 Mon Sep 17 00:00:00 2001 From: getzze Date: Thu, 19 Nov 2020 23:21:09 +0000 Subject: [PATCH 4/7] add tricky list comprehension examples --- corner_cases.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/corner_cases.jl b/corner_cases.jl index 6354d37..a841ddb 100644 --- a/corner_cases.jl +++ b/corner_cases.jl @@ -397,3 +397,14 @@ 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 From 4f1d5189233ee3b6ded3ff11220bb66c2e95a94e Mon Sep 17 00:00:00 2001 From: getzze Date: Fri, 20 Nov 2020 12:40:09 +0000 Subject: [PATCH 5/7] add arrow and pair operator followed by symbol examples --- corner_cases.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/corner_cases.jl b/corner_cases.jl index a841ddb..9b68175 100644 --- a/corner_cases.jl +++ b/corner_cases.jl @@ -305,6 +305,13 @@ a = :(x = 2) # 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 From 22e9fc78792fa27ec486f578a9562eabdb2ee565 Mon Sep 17 00:00:00 2001 From: getzze Date: Wed, 3 Aug 2022 10:32:35 +0100 Subject: [PATCH 6/7] parse apostrophe as char and not adjoint after exclamation --- corner_cases.jl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/corner_cases.jl b/corner_cases.jl index 9b68175..93fdc1c 100644 --- a/corner_cases.jl +++ b/corner_cases.jl @@ -152,11 +152,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` From a609495d94a8704827d1f3b71edbf7f4616aba43 Mon Sep 17 00:00:00 2001 From: getzze Date: Fri, 5 Aug 2022 10:15:58 +0100 Subject: [PATCH 7/7] test apostrophe as adjoint --- corner_cases.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/corner_cases.jl b/corner_cases.jl index 93fdc1c..34ec449 100644 --- a/corner_cases.jl +++ b/corner_cases.jl @@ -135,6 +135,8 @@ 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