-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
2 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
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,66 @@ | ||
use ropey::{Rope, RopeSlice}; | ||
|
||
pub fn detect_language(inital_guess: Option<&str>, content: Rope) -> Option<&'static str> { | ||
tracing::info!("inital_guess: {inital_guess:?}"); | ||
if inital_guess == Some("c") { | ||
let cpp_markers = [ | ||
"public", | ||
"protected", | ||
"private", | ||
"std::", | ||
"dynamic_cast", | ||
"static_cast", | ||
"reinterpret_cast", | ||
"#include <iostream>", | ||
"#include <vector>", | ||
"#include <string>", | ||
"class", | ||
"throw", | ||
"catch", | ||
"try", | ||
"nullptr", | ||
"const&", | ||
"final", | ||
]; | ||
if detect_markers(content.slice(..), &cpp_markers) > 3 { | ||
return Some("cpp"); | ||
} | ||
} | ||
|
||
detect_shebang(content.slice(..)) | ||
} | ||
|
||
fn detect_shebang(content: RopeSlice) -> Option<&'static str> { | ||
let first_line = content | ||
.slice(..content.len_chars().min(1000)) | ||
.get_line(0)? | ||
.to_string(); | ||
|
||
let shebangs = [ | ||
("python3", "python"), | ||
("python2", "python"), | ||
("python", "python"), | ||
("#!/bin/bash", "bash"), | ||
("#!/usr/bin/bash", "bash"), | ||
("#!/bin/sh", "bash"), | ||
("#!/usr/bin/env bash", "bash"), | ||
("zsh", "bash"), | ||
]; | ||
|
||
for (shebang, language) in shebangs { | ||
if first_line.contains(shebang) { | ||
return Some(language); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn detect_markers(content: RopeSlice, markers: &[&str]) -> usize { | ||
let start = content.slice(..content.len_chars().min(1000)).to_string(); | ||
let mut count = 0; | ||
for marker in markers { | ||
count += start.contains(marker) as usize; | ||
} | ||
count | ||
} |
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,14 @@ | ||
|
||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
|
||
class Thing { | ||
public: | ||
int thing; | ||
} | ||
|
||
int main() { | ||
std::cout << "hello" << std::endl; | ||
} |