mirror of https://github.com/lapce/lapce.git
add js,ts,py syntax support
This commit is contained in:
parent
ea92439dfe
commit
bf2e4959fd
|
@ -2008,7 +2008,9 @@ dependencies = [
|
|||
"tree-sitter-go",
|
||||
"tree-sitter-highlight",
|
||||
"tree-sitter-javascript",
|
||||
"tree-sitter-python",
|
||||
"tree-sitter-rust",
|
||||
"tree-sitter-typescript",
|
||||
"xi-rope",
|
||||
]
|
||||
|
||||
|
@ -4486,6 +4488,16 @@ dependencies = [
|
|||
"tree-sitter",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tree-sitter-python"
|
||||
version = "0.19.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "83c46916553ebc2a5b23763cd2da8d2b104c515c8f828eb678d1477ccd8c379c"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"tree-sitter",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tree-sitter-rust"
|
||||
version = "0.20.0"
|
||||
|
@ -4496,6 +4508,16 @@ dependencies = [
|
|||
"tree-sitter",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tree-sitter-typescript"
|
||||
version = "0.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8935efd97c92067c9b2b6d7acb647607590996ba80f3a7be09a197f9c1fdab73"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"tree-sitter",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "try-lock"
|
||||
version = "0.2.3"
|
||||
|
|
|
@ -14,4 +14,6 @@ tree-sitter-highlight = "0.20.1"
|
|||
tree-sitter-rust = "0.20.0"
|
||||
tree-sitter-go = "0.19.1"
|
||||
tree-sitter-javascript = "0.20.0"
|
||||
tree-sitter-typescript = "0.20.0"
|
||||
tree-sitter-python = "0.19.1"
|
||||
xi-rope = { git = "https://github.com/lapce/xi-editor", features = ["serde"] }
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
use crate::style::HighlightConfiguration;
|
||||
|
||||
const DEFAULT_CODE_LENS_LIST: &[&str] = &["source_file"];
|
||||
const DEFAULT_CODE_LENS_IGNORE_LIST: &[&str] = &["source_file"];
|
||||
const RUST_CODE_LENS_LIST: &[&str] =
|
||||
&["source_file", "impl_item", "trait_item", "declaration_list"];
|
||||
const RUST_CODE_LENS_IGNORE_LIST: &[&str] =
|
||||
|
@ -22,6 +24,11 @@
|
|||
pub enum LapceLanguage {
|
||||
Rust,
|
||||
Go,
|
||||
Javascript,
|
||||
Jsx,
|
||||
Typescript,
|
||||
Tsx,
|
||||
Python,
|
||||
}
|
||||
|
||||
impl LapceLanguage {
|
||||
|
@ -29,12 +36,12 @@ pub fn from_path(path: &Path) -> Option<LapceLanguage> {
|
|||
let extension = path.extension()?.to_str()?;
|
||||
Some(match extension {
|
||||
"rs" => LapceLanguage::Rust,
|
||||
// "js" => LapceLanguage::Javascript,
|
||||
// "jsx" => LapceLanguage::Javascript,
|
||||
"js" => LapceLanguage::Javascript,
|
||||
"jsx" => LapceLanguage::Jsx,
|
||||
"ts" => LapceLanguage::Typescript,
|
||||
"tsx" => LapceLanguage::Tsx,
|
||||
"go" => LapceLanguage::Go,
|
||||
// "toml" => LapceLanguage::Toml,
|
||||
// "yaml" => LapceLanguage::Yaml,
|
||||
// "yml" => LapceLanguage::Yaml,
|
||||
"py" => LapceLanguage::Python,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
@ -43,6 +50,13 @@ fn tree_sitter_language(&self) -> tree_sitter::Language {
|
|||
match self {
|
||||
LapceLanguage::Rust => tree_sitter_rust::language(),
|
||||
LapceLanguage::Go => tree_sitter_go::language(),
|
||||
LapceLanguage::Javascript => tree_sitter_javascript::language(),
|
||||
LapceLanguage::Jsx => tree_sitter_javascript::language(),
|
||||
LapceLanguage::Typescript => {
|
||||
tree_sitter_typescript::language_typescript()
|
||||
}
|
||||
LapceLanguage::Tsx => tree_sitter_typescript::language_tsx(),
|
||||
LapceLanguage::Python => tree_sitter_python::language(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,6 +72,11 @@ pub(crate) fn new_highlight_config(&self) -> HighlightConfiguration {
|
|||
let query = match self {
|
||||
LapceLanguage::Rust => tree_sitter_rust::HIGHLIGHT_QUERY,
|
||||
LapceLanguage::Go => tree_sitter_go::HIGHLIGHT_QUERY,
|
||||
LapceLanguage::Javascript => tree_sitter_javascript::HIGHLIGHT_QUERY,
|
||||
LapceLanguage::Jsx => tree_sitter_javascript::JSX_HIGHLIGHT_QUERY,
|
||||
LapceLanguage::Typescript => tree_sitter_typescript::HIGHLIGHT_QUERY,
|
||||
LapceLanguage::Tsx => tree_sitter_typescript::HIGHLIGHT_QUERY,
|
||||
LapceLanguage::Python => tree_sitter_python::HIGHLIGHT_QUERY,
|
||||
};
|
||||
|
||||
HighlightConfiguration::new(language, query, "", "").unwrap()
|
||||
|
@ -71,6 +90,7 @@ pub(crate) fn walk_tree(
|
|||
let (list, ignore_list) = match self {
|
||||
LapceLanguage::Rust => (RUST_CODE_LENS_LIST, RUST_CODE_LENS_IGNORE_LIST),
|
||||
LapceLanguage::Go => (GO_CODE_LENS_LIST, GO_CODE_LENS_IGNORE_LIST),
|
||||
_ => (DEFAULT_CODE_LENS_LIST, DEFAULT_CODE_LENS_IGNORE_LIST),
|
||||
};
|
||||
walk_tree(cursor, 0, normal_lines, list, ignore_list);
|
||||
}
|
||||
|
|
|
@ -1,102 +0,0 @@
|
|||
use lazy_static::lazy_static;
|
||||
use std::path::Path;
|
||||
use tree_sitter::Parser;
|
||||
use tree_sitter_highlight::HighlightConfiguration;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref SCOPES: Vec<String> = vec![
|
||||
"constant".to_string(),
|
||||
"constant.builtin".to_string(),
|
||||
"type".to_string(),
|
||||
"type.builtin".to_string(),
|
||||
"property".to_string(),
|
||||
"comment".to_string(),
|
||||
"constructor".to_string(),
|
||||
"function".to_string(),
|
||||
"function.method".to_string(),
|
||||
"function.macro".to_string(),
|
||||
"punctuation.bracket".to_string(),
|
||||
"punctuation.delimiter".to_string(),
|
||||
"label".to_string(),
|
||||
"keyword".to_string(),
|
||||
"string".to_string(),
|
||||
"variable.parameter".to_string(),
|
||||
"variable.builtin".to_string(),
|
||||
"variable.other.member".to_string(),
|
||||
"operator".to_string(),
|
||||
"attribute".to_string(),
|
||||
"escape".to_string(),
|
||||
];
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
|
||||
pub enum LapceLanguage {
|
||||
Rust,
|
||||
Javascript,
|
||||
Go,
|
||||
}
|
||||
|
||||
impl LapceLanguage {
|
||||
pub fn from_path(path: &Path) -> Option<LapceLanguage> {
|
||||
let extension = path.extension()?.to_str()?;
|
||||
Some(match extension {
|
||||
"rs" => LapceLanguage::Rust,
|
||||
"js" => LapceLanguage::Javascript,
|
||||
"jsx" => LapceLanguage::Javascript,
|
||||
"go" => LapceLanguage::Go,
|
||||
// "toml" => LapceLanguage::Toml,
|
||||
// "yaml" => LapceLanguage::Yaml,
|
||||
// "yml" => LapceLanguage::Yaml,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_highlight_config(language: LapceLanguage) -> HighlightConfiguration {
|
||||
match language {
|
||||
LapceLanguage::Rust => {
|
||||
let mut configuration = HighlightConfiguration::new(
|
||||
tree_sitter_rust::language(),
|
||||
tree_sitter_rust::HIGHLIGHT_QUERY,
|
||||
"",
|
||||
"",
|
||||
)
|
||||
.unwrap();
|
||||
configuration.configure(&SCOPES);
|
||||
configuration
|
||||
}
|
||||
LapceLanguage::Javascript => {
|
||||
let mut configuration = HighlightConfiguration::new(
|
||||
tree_sitter_javascript::language(),
|
||||
tree_sitter_javascript::HIGHLIGHT_QUERY,
|
||||
"",
|
||||
"",
|
||||
)
|
||||
.unwrap();
|
||||
configuration.configure(&SCOPES);
|
||||
configuration
|
||||
}
|
||||
LapceLanguage::Go => {
|
||||
let mut configuration = HighlightConfiguration::new(
|
||||
tree_sitter_go::language(),
|
||||
tree_sitter_go::HIGHLIGHT_QUERY,
|
||||
"",
|
||||
"",
|
||||
)
|
||||
.unwrap();
|
||||
configuration.configure(&SCOPES);
|
||||
configuration
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_parser(language: LapceLanguage) -> Parser {
|
||||
let language = match language {
|
||||
LapceLanguage::Rust => tree_sitter_rust::language(),
|
||||
LapceLanguage::Javascript => tree_sitter_javascript::language(),
|
||||
LapceLanguage::Go => tree_sitter_go::language(),
|
||||
};
|
||||
let mut parser = Parser::new();
|
||||
parser.set_language(language).unwrap();
|
||||
parser
|
||||
}
|
Loading…
Reference in New Issue