Repository: lapce/lapce
File: lapce-core/src/language.rs (Rust)

Code before:
                return Some(properties.id);
            }
        }
        None
    }

    // NOTE: Instead of using `&LANGUAGES[*self as usize]` directly, the
    // `debug_assertion` gives better feedback should something has gone wrong
    // badly.
    fn properties(&self) -> &SyntaxProperties {
        let i = *self as usize;
        let l = &LANGUAGES[i];
        debug_assert!(
            l.id == *self,
            "LANGUAGES[{i}]: Setting::id mismatch: {:?} != {:?}",
            l.id,
            self
        );
        l
    }

    pub fn comment_token(&self) -> &str {
        self.properties().comment
    }

    pub fn indent_unit(&self) -> &str {
        self.properties().indent
    }

    pub(crate) fn new_parser(&self) -> Parser {
        let language = (self.properties().language)();
        let mut parser = Parser::new();
        parser.set_language(language).unwrap();
        parser
    }

    pub(crate) fn new_highlight_config(&self) -> HighlightConfiguration {
        let language = (self.properties().language)();
        let query = self.properties().highlight;

        HighlightConfiguration::new(language, query, "", "").unwrap()
    }

    pub(crate) fn walk_tree(
        &self,
        cursor: &mut TreeCursor,
        normal_lines: &mut HashSet<usize>,
    ) {
        let (list, ignore_list) = self.properties().code_lens;
        walk_tree(cursor, normal_lines, list, ignore_list);
    }

Review comment (suggestion):
there's another extension here, `lhs` which I believe would require special treatment that our current system does not allow. This is because in `lhs` files, comments are implicit and you must mark code with a special symbol

Code after:
        language: tree_sitter_swift::language,
        highlight: tree_sitter_swift::HIGHLIGHTS_QUERY,
        comment: "//",
        indent: "  ",
        code_lens: (DEFAULT_CODE_LENS_LIST, DEFAULT_CODE_LENS_IGNORE_LIST),
        extensions: &["swift"],
    },
    #[cfg(feature = "lang-ql")]
    SyntaxProperties {
        id: LapceLanguage::QL,
        language: tree_sitter_ql::language,
        highlight: tree_sitter_ql::HIGHLIGHTS_QUERY,
        comment: "//",
        indent: "  ",
        code_lens: (DEFAULT_CODE_LENS_LIST, DEFAULT_CODE_LENS_IGNORE_LIST),
        extensions: &["ql"],
    },
    #[cfg(feature = "lang-haskell")]
    SyntaxProperties {
        id: LapceLanguage::Haskell,
        language: tree_sitter_haskell::language,
        highlight: tree_sitter_haskell::HIGHLIGHTS_QUERY,
        comment: "--",
        indent: "  ",
        code_lens: (DEFAULT_CODE_LENS_LIST, DEFAULT_CODE_LENS_IGNORE_LIST),
        extensions: &["hs"],
    },
];

impl LapceLanguage {
    pub fn from_path(path: &Path) -> Option<LapceLanguage> {
        let extension = path.extension()?.to_str()?.to_lowercase();
        // NOTE: This is a linear search.  It is assumed that this function
        // isn't called in any tight loop.
        for properties in LANGUAGES {
            if properties.extensions.contains(&extension.as_str()) {
                return Some(properties.id);
            }
        }
        None
    }

    // NOTE: Instead of using `&LANGUAGES[*self as usize]` directly, the
    // `debug_assertion` gives better feedback should something has gone wrong
    // badly.
    fn properties(&self) -> &SyntaxProperties {
        let i = *self as usize;
        let l = &LANGUAGES[i];
        debug_assert!(
            l.id == *self,
            "LANGUAGES[{i}]: Setting::id mismatch: {:?} != {:?}",