-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: jansul <[email protected]> Co-authored-by: Sijawusz Pur Rahnama <[email protected]>
- Loading branch information
1 parent
ab00397
commit 1a9f3f4
Showing
34 changed files
with
616 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* Some comment. */ | ||
record Article { | ||
id : Number, | ||
description : String, | ||
title : String | ||
} | ||
-----------------------------------------------------------------file test.mint | ||
{ | ||
"id": 0, | ||
"method": "initialize", | ||
"params": { | ||
"capabilities": { | ||
"textDocument": { | ||
"semanticTokens": { | ||
"dynamicRegistration": false, | ||
"tokenTypes": ["property"] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
-------------------------------------------------------------------------request | ||
{ | ||
"jsonrpc": "2.0", | ||
"id": 1, | ||
"params": { | ||
"textDocument": { | ||
"uri": "file://#{root_path}/test.mint" | ||
} | ||
}, | ||
"method": "textDocument/semanticTokens/full" | ||
} | ||
-------------------------------------------------------------------------request | ||
{ | ||
"jsonrpc": "2.0", | ||
"result": { | ||
"data": [ | ||
0, | ||
0, | ||
20, | ||
5, | ||
0, | ||
1, | ||
0, | ||
6, | ||
4, | ||
0, | ||
0, | ||
7, | ||
7, | ||
1, | ||
0, | ||
1, | ||
2, | ||
2, | ||
6, | ||
0, | ||
0, | ||
5, | ||
6, | ||
1, | ||
0, | ||
1, | ||
2, | ||
11, | ||
6, | ||
0, | ||
0, | ||
14, | ||
6, | ||
1, | ||
0, | ||
1, | ||
2, | ||
5, | ||
6, | ||
0, | ||
0, | ||
8, | ||
6, | ||
1, | ||
0 | ||
] | ||
}, | ||
"id": 1 | ||
} | ||
------------------------------------------------------------------------response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Mint | ||
class Ast | ||
module Directives | ||
class Highlight < Node | ||
getter content | ||
|
||
def initialize(@content : Block, | ||
@input : Data, | ||
@from : Int32, | ||
@to : Int32) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Mint | ||
class Cli < Admiral::Command | ||
class Highlight < Admiral::Command | ||
include Command | ||
|
||
define_help description: "Returns the syntax highlighted version of the given file" | ||
|
||
define_argument path, description: "The path to the file" | ||
|
||
define_flag html : Bool, | ||
description: "If specified, print the highlighted code as HTML", | ||
default: false | ||
|
||
def run | ||
return unless path = arguments.path | ||
puts SemanticTokenizer.highlight(path, flags.html) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Mint | ||
class Compiler | ||
def _compile(node : Ast::Directives::Highlight) : String | ||
content = | ||
compile node.content | ||
|
||
formatted = | ||
Formatter.new.format(node.content, Formatter::BlockFormat::Naked) | ||
|
||
parser = Parser.new(formatted, "source.mint") | ||
parser.code_block_naked | ||
|
||
parts = | ||
SemanticTokenizer.tokenize(parser.ast) | ||
|
||
mapped = | ||
parts.map do |item| | ||
case item | ||
in String | ||
"`#{skip { escape_for_javascript(item) }}`" | ||
in Tuple(SemanticTokenizer::TokenType, String) | ||
"_h('span', { className: '#{item[0].to_s.underscore}' }, [`#{skip { escape_for_javascript(item[1]) }}`])" | ||
end | ||
end | ||
|
||
"[#{content}, _h(React.Fragment, {}, [#{mapped.join(",\n")}])]" | ||
end | ||
|
||
def escape_for_javascript(value : String) | ||
value | ||
.gsub('\\', "\\\\") | ||
.gsub('`', "\\`") | ||
.gsub("${", "\\${") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Mint | ||
class Formatter | ||
def format(node : Ast::Directives::Highlight) | ||
"@highlight #{format(node.content)}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Mint | ||
module LS | ||
class Definition < LSP::RequestMessage | ||
def definition(node : Ast::TypeId, server : Server, workspace : Workspace, stack : Array(Ast::Node)) | ||
found = | ||
workspace.ast.enums.find(&.name.value.==(node.value)) || | ||
workspace.ast.records.find(&.name.value.==(node.value)) || | ||
workspace.ast.stores.find(&.name.value.==(node.value)) || | ||
find_component(workspace, node.value) | ||
|
||
if found.nil? && (next_node = stack[1]) | ||
return definition(next_node, server, workspace, stack) | ||
end | ||
|
||
return if Core.ast.nodes.includes?(found) | ||
|
||
case found | ||
when Ast::Store, Ast::Enum, Ast::Component, Ast::RecordDefinition | ||
location_link server, node, found.name, found | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module Mint | ||
module LS | ||
# This is the class that handles the "textDocument/semanticTokens/full" request. | ||
class SemanticTokens < LSP::RequestMessage | ||
property params : LSP::SemanticTokensParams | ||
|
||
def execute(server) | ||
uri = | ||
URI.parse(params.text_document.uri) | ||
|
||
ast = | ||
Workspace[uri.path.to_s][uri.path.to_s] | ||
|
||
# This is used later on to convert the line/column of each token | ||
input = | ||
ast.nodes.first.input | ||
|
||
tokenizer = SemanticTokenizer.new | ||
tokenizer.tokenize(ast) | ||
|
||
data = | ||
tokenizer.tokens.sort_by(&.from).compact_map do |token| | ||
location = | ||
Ast::Node.compute_location(input, token.from, token.to) | ||
|
||
type = | ||
token.type.to_s.underscore | ||
|
||
if index = SemanticTokenizer::TOKEN_TYPES.index(type) | ||
[ | ||
location.start[0] - 1, | ||
location.start[1], | ||
token.to - token.from, | ||
index, | ||
0, | ||
] | ||
end | ||
end | ||
|
||
result = [] of Array(Int32) | ||
|
||
data.each_with_index do |item, index| | ||
current = | ||
item.dup | ||
|
||
unless index.zero? | ||
last = | ||
data[index - 1] | ||
|
||
current[0] = | ||
current[0] - last[0] | ||
|
||
current[1] = current[1] - last[1] if current[0] == 0 | ||
end | ||
|
||
result << current | ||
end | ||
|
||
{ | ||
data: result.flatten, | ||
} | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.