Skip to content

Commit 8052adb

Browse files
committed
fix(github_expansion): match language optionally, handle single lines correctly
1 parent da42e7e commit 8052adb

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/handlers/github_expansion.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@ use anyhow::Result;
77
use once_cell::sync::Lazy;
88

99
static GITHUB: Lazy<Regex> = Lazy::new(|| {
10-
Regex::new(r"https?://github\.com/(?P<repo>[\w-]+/[\w.-]+)/blob/(?P<ref>.+?)/(?P<file>.*\.(?:(?P<language>\w+))?)#L(?P<start>\d+)(?:[~-]L?(?P<end>\d+)?)?").unwrap()
10+
Regex::new(r"https?://github\.com/(?P<repo>[\w-]+/[\w.-]+)/blob/(?P<ref>.+?)/(?P<file>.*)#L(?P<start>\d+)(?:[~-]L?(?P<end>\d+)?)?").unwrap()
1111
});
1212

1313
pub async fn handle(message: &serenity::Message, ctx: &serenity::Context) -> Result<()> {
1414
let message = message.clone();
1515
let mut embeds: Vec<serenity::CreateEmbed> = vec![];
1616

1717
for captures in GITHUB.captures_iter(&message.content) {
18-
let repo = &captures["repo"];
19-
let ref_ = &captures["ref"];
20-
let file = &captures["file"];
18+
let repo = captures["repo"].to_owned();
19+
let ref_ = captures["ref"].to_owned();
20+
let file = captures["file"].to_owned();
2121

22-
let language = match captures.name("language") {
23-
Some(m) => m.as_str().to_owned(),
24-
None => "".to_owned(),
25-
};
22+
let language = file.split('.').last().unwrap_or("").to_owned();
2623

27-
let start = &captures["start"].parse::<usize>()?;
24+
let start = captures["start"].parse::<usize>()?;
2825
let end = captures
2926
.name("end")
3027
.and_then(|end| end.as_str().parse::<usize>().ok());
@@ -46,7 +43,7 @@ pub async fn handle(message: &serenity::Message, ctx: &serenity::Context) -> Res
4643
let idx_start = start - 1;
4744
let idx_end = match end {
4845
Some(end) => end,
49-
None => lines.len(),
46+
None => start,
5047
};
5148
let selected_lines = &lines[idx_start..idx_end];
5249

0 commit comments

Comments
 (0)