Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.
Merged
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
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/bleep/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ tree-sitter-c-sharp = "0.20.0"
tree-sitter-java = { git = "https://github.com/tree-sitter/tree-sitter-java", tag = "v0.20.0" }
tree-sitter-cpp = { git = "https://github.com/tree-sitter/tree-sitter-cpp", rev = "5ead1e2" }
tree-sitter-ruby = "0.20.0"
tree-sitter-r = "0.19.5"
petgraph = { version = "0.6.3", default-features = false, features = ["serde-1"] }

# webserver
Expand Down
2 changes: 2 additions & 0 deletions server/bleep/src/intelligence/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod go;
mod java;
mod javascript;
mod python;
mod r;
mod ruby;
mod rust;
mod typescript;
Expand All @@ -28,6 +29,7 @@ pub static ALL_LANGUAGES: &[&TSLanguageConfig] = &[
&java::JAVA,
&cpp::CPP,
&ruby::RUBY,
&r::R,
];

/// A generic language wrapper type.
Expand Down
254 changes: 254 additions & 0 deletions server/bleep/src/intelligence/language/r/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
use crate::intelligence::{MemoizedQuery, TSLanguageConfig};

pub static R: TSLanguageConfig = TSLanguageConfig {
language_ids: &["R"],
file_extensions: &["R"],
grammar: tree_sitter_r::language,
scope_query: MemoizedQuery::new(include_str!("./scopes.scm")),
hoverable_query: MemoizedQuery::new(
r#"
(identifier) @hoverable
"#,
),
namespaces: &[&[
// variables
"variable",
]],
};

#[cfg(test)]
mod tests {
use crate::intelligence::language::test_utils::*;

// Self::method and self.method can be raised as references
#[test]
fn declarations() {
test_scopes(
"R",
r#"
x <- value
value -> y
x[0] <<- value
value ->> y[0]
"#
.as_bytes(),
expect![[r#"
scope {
definitions: [
x {
kind: "variable",
context: "§x§ <- value",
referenced in (1): [
`§x§[0] <<- value`,
],
},
y {
kind: "variable",
context: "value -> §y§",
referenced in (1): [
`value ->> §y§[0]`,
],
},
],
child scopes: [],
}
"#]],
)
}

#[test]
fn control_if() {
test_scopes(
"R",
r#"
x <- TRUE
y <- value
if (x)
return(y)
else
return(y)
"#
.as_bytes(),
expect![[r#"
scope {
definitions: [
x {
kind: "variable",
context: "§x§ <- TRUE",
referenced in (1): [
`if (§x§)`,
],
},
y {
kind: "variable",
context: "§y§ <- value",
referenced in (2): [
`return(§y§)`,
`return(§y§)`,
],
},
],
child scopes: [],
}
"#]],
)
}

#[test]
fn control_loop() {
test_scopes(
"R",
r#"
x <- TRUE
repeat x

while (x) return

y <- c(1, 2, 3)
for (item in y) {
y <- item + 1
}
"#
.as_bytes(),
expect![[r#"
scope {
definitions: [
x {
kind: "variable",
context: "§x§ <- TRUE",
referenced in (2): [
`repeat §x§`,
`while (§x§) return`,
],
},
y {
kind: "variable",
context: "§y§ <- c(1, 2, 3)",
referenced in (1): [
`for (item in §y§) {`,
],
},
],
child scopes: [
scope {
definitions: [
item {
kind: "variable",
context: "for (§item§ in y) {",
referenced in (1): [
`y <- §item§ + 1`,
],
},
],
child scopes: [
scope {
definitions: [
y {
kind: "variable",
context: "§y§ <- item + 1",
},
],
child scopes: [],
},
],
},
],
}
"#]],
)
}

#[test]
fn control_switch() {
test_scopes(
"R",
r#"
x <- "add"

y <- 2
z <- 1
switch(x, "add" = y + z, "subtract" = y - z)
"#
.as_bytes(),
expect![[r#"
scope {
definitions: [
x {
kind: "variable",
context: "§x§ <- \"add\"",
referenced in (1): [
`switch(§x§, "add" = y + z, "subtract" = y - z)`,
],
},
y {
kind: "variable",
context: "§y§ <- 2",
referenced in (2): [
`switch(x, "add" = §y§ + z, "subtract" = y - z)`,
`switch(x, "add" = y + z, "subtract" = §y§ - z)`,
],
},
z {
kind: "variable",
context: "§z§ <- 1",
referenced in (2): [
`switch(x, "add" = y + §z§, "subtract" = y - z)`,
`switch(x, "add" = y + z, "subtract" = y - §z§)`,
],
},
],
child scopes: [],
}
"#]],
)
}

#[test]
fn indexing() {
test_scopes(
"R",
r#"
x <- c(1, 2, 3)

idx <- 1

y <- x[i]
z <- x $ i
w <- x[[i]]
"#
.as_bytes(),
expect![[r#"
scope {
definitions: [
x {
kind: "variable",
context: "§x§ <- c(1, 2, 3)",
referenced in (3): [
`y <- §x§[i]`,
`z <- §x§ $ i`,
`w <- §x§[[i]]`,
],
},
idx {
kind: "variable",
context: "§idx§ <- 1",
},
y {
kind: "variable",
context: "§y§ <- x[i]",
},
z {
kind: "variable",
context: "§z§ <- x $ i",
},
w {
kind: "variable",
context: "§w§ <- x[[i]]",
},
],
child scopes: [],
}
"#]],
)
}
}
Loading