<commit_before>use crate::language_client::LanguageClient;
use anyhow::Result;
use lsp_types::Command;
use serde::Deserialize;
use serde_json::Value;

pub mod command {
    pub const TEST: &str = "test";
    pub const GENERATE: &str = "generate";
}

impl LanguageClient {
    pub fn handle_gopls_command(&self, cmd: &Command) -> Result<bool> {
        match cmd.command.as_str() {
            command::TEST => {
                if let Some(args) = &cmd.arguments {
                    if let Some(file) = args.get(0) {
                        let file = String::deserialize(file)?;
                        let path = parse_package_path(file.as_str()).unwrap_or("./...".into());
                        let run = <Option<Vec<String>>>::deserialize(
                            args.get(1).unwrap_or(&Value::Null),
                        )?;

                        let bench = <Option<Vec<String>>>::deserialize(
                            args.get(2).unwrap_or(&Value::Null),
                        )?;

                        let run = run.unwrap_or_default();
                        let bench = bench.unwrap_or_default();

                        if run.len() > 0 {
                            let cmd = format!("term go test -run '{}' {}", run.join("|"), path);
                            self.vim()?.command(cmd)?;
                        } else if bench.len() > 0 {
                            let cmd = format!("term go test -bench '{}' {}", bench.join("|"), path);
                            self.vim()?.command(cmd)?;
                        } else {
                            self.vim()?.echoerr("No tests to run")?;
                        }
                    }
                }
            }
            command::GENERATE => {
                if let Some(arguments) = &cmd.arguments {
                    if let Some(package) = arguments.get(0) {
                        let package = String::deserialize(package)?;
                        let recursive =
                            bool::deserialize(arguments.get(1).unwrap_or(&Value::Bool(false)))?;
                        let cmd = match (package, recursive) {
                            (package, false) => format!("term go generate -x {}", package),
                            (_, true) => "term go generate -x ./...".into(),
                        };
                        self.vim()?.command(cmd)?;
                    }
                }
            }

            _ => return Ok(false),
        }

        Ok(true)
    }
}

fn parse_package_path(path: &str) -> Option<String> {
    let path = if path.starts_with("file://") {
        path.strip_prefix("file://")?
    } else {
        path
    };
    let path = std::path::PathBuf::from(path);
    Some(path.parent()?.to_str()?.to_owned())
}<commit_msg>Fix gopls codeLens commands to include the `gopls` prefix that it now sends (#1131)<commit_after>use crate::language_client::LanguageClient;
use anyhow::Result;
use lsp_types::Command;
use serde::Deserialize;
use serde_json::Value;

pub mod command {
    // older versions of gopls send code lens commands without the `gopls` prefix
    // so probably a good idea to maintain both for a while.
    pub(super) const TEST: &str = "test";
    pub(super) const GOPLS_TEST: &str = "gopls.test";
    pub(super) const GENERATE: &str = "generate";
    pub(super) const GOPLS_GENERATE: &str = "gopls.generate";
}

impl LanguageClient {
    pub fn handle_gopls_command(&self, cmd: &Command) -> Result<bool> {
        match cmd.command.as_str() {
            command::TEST | command::GOPLS_TEST => {
                if let Some(args) = &cmd.arguments {
                    if let Some(file) = args.get(0) {
                        let file = String::deserialize(file)?;
                        let path = parse_package_path(file.as_str()).unwrap_or("./...".into());
                        let run = <Option<Vec<String>>>::deserialize(
                            args.get(1).unwrap_or(&Value::Null),
                        )?;

                        let bench = <Option<Vec<String>>>::deserialize(
                            args.get(2).unwrap_or(&Value::Null),
                        )?;

                        let run = run.unwrap_or_default();
                        let bench = bench.unwrap_or_default();

                        if run.len() > 0 {
                            let cmd = format!("term go test -run '{}' {}", run.join("|"), path);
                            self.vim()?.command(cmd)?;
                        } else if bench.len() > 0 {
                            let cmd = format!("term go test -bench '{}' {}", bench.join("|"), path);
                            self.vim()?.command(cmd)?;
                        } else {
                            self.vim()?.echoerr("No tests to run")?;
                        }
                    }
                }
            }
            command::GENERATE | command::GOPLS_GENERATE => {
                if let Some(arguments) = &cmd.arguments {
                    if let Some(package) = arguments.get(0) {
                        let package = String::deserialize(package)?;
                        let recursive =
                            bool::deserialize(arguments.get(1).unwrap_or(&Value::Bool(false)))?;
                        let cmd = match (package, recursive) {
                            (package, false) => format!("term go generate -x {}", package),
                            (_, true) => "term go generate -x ./...".into(),
                        };
                        self.vim()?.command(cmd)?;
                    }
                }
            }

            _ => return Ok(false),
        }

        Ok(true)
    }
}

fn parse_package_path(path: &str) -> Option<String> {
    let path = if path.starts_with("file://") {
        path.strip_prefix("file://")?
    } else {
        path
    };
    let path = std::path::PathBuf::from(path);
    Some(path.parent()?.to_str()?.to_owned())
}
{'fraction_non_alphanumeric': 0.14120796386146897, 'fraction_numerical': 0.0030115442529697173, 'mean_word_length': 1.3278816199376946, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4704171', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1572, 'n_words': 336}