Skip to content

Update files to julia-1.0+ and add some corner cases #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 181 additions & 8 deletions corner_cases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -75,13 +78,17 @@ bar"
x = """hello "world" foobar"""
y = """foo\\"""
z = """bar\""""
w = """"bar"""

# Expected: highlight `$user`
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"

Expand Down Expand Up @@ -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`
Expand All @@ -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`
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
28 changes: 19 additions & 9 deletions test_file.jl
Original file line number Diff line number Diff line change
@@ -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!`.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down