Skip to content

Commit

Permalink
wip: Clean up, remove old code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Sep 30, 2023
1 parent 1919cf4 commit a2135a2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 206 deletions.
159 changes: 4 additions & 155 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use betterletters::scoping::{
langs::python::{PremadePythonQuery, Python},
literal::Literal,
ScopedViewBuildStep, ScoperBuildError,
};
use betterletters::scoping::{literal::Literal, ScopedViewBuildStep, ScoperBuildError};
#[cfg(feature = "deletion")]
use betterletters::stages::DeletionStage;
#[cfg(feature = "german")]
Expand Down Expand Up @@ -189,29 +185,12 @@ fn level_filter_from_env_and_verbosity(additional_verbosity: u8) -> LevelFilter
}

mod cli {
use std::{
error::Error,
fmt::{self, Display},
str::FromStr,
thread,
time::Duration,
};

use betterletters::{
scoping::langs::{
python::{CustomPythonQuery, PremadePythonQuery, Python, PythonQuery},
LanguageScoperError,
},
scoping::langs::python::{CustomPythonQuery, PremadePythonQuery},
GLOBAL_SCOPE,
};
use clap::{
builder::{ArgPredicate, TypedValueParser},
error::{ContextKind, ContextValue, ErrorKind},
ArgAction, Parser, ValueEnum,
};
use enum_iterator::all;
use itertools::Itertools;
use tree_sitter::{Query, QueryError};
use clap::{builder::ArgPredicate, ArgAction, Parser};
use tree_sitter::QueryError;

/// Main CLI entrypoint.
///
Expand Down Expand Up @@ -377,45 +356,6 @@ mod cli {
pub squeeze: bool,
}

// #[derive(ValueEnum, Debug, Clone, Copy)]
// pub(super) enum PythonQuery {
// Query,
// Comments,
// DocStrings,
// }

// impl fmt::Display for PythonQuery {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// match self {
// PythonQuery::Query => write!(f, "query"),
// PythonQuery::Comments => write!(f, "comments"),
// PythonQuery::DocStrings => write!(f, "doc-strings"),
// }
// }
// }

// use betterletters::scoping::langs::python::PremadeQuery;

// impl Into<PremadeQuery> for PythonQuery {
// fn into(self) -> PremadeQuery {
// match self {
// PythonQuery::Query => PremadeQuery::Comments,
// PythonQuery::Comments => PremadeQuery::Comments,
// PythonQuery::DocStrings => PremadeQuery::DocStrings,
// }
// }
// }

// impl From<PythonQuery> for PremadeQuery {
// fn from(value: PythonQuery) -> Self {
// match value {
// PythonQuery::Query => PremadeQuery::Comments,
// PythonQuery::Comments => PremadeQuery::Comments,
// PythonQuery::DocStrings => PremadeQuery::DocStrings,
// }
// }
// }

#[derive(Parser, Debug)]
#[group(required = false, multiple = false)]
#[command(next_help_heading = "Language scopes")]
Expand All @@ -439,97 +379,6 @@ mod cli {
T::try_from(value.to_string()).map_err(|e| e.to_string())
}

// #[derive(Clone)]
// struct LanguageScoperParser<T> {
// lang: T,
// }
// <T: FromRawQuery + Clone>;

// impl<T: FromRawQuery + Clone + Sync + Send + 'static> TypedValueParser for LanguageScoperParser<T> {
// type Value = T;

// fn parse_ref(
// &self,
// cmd: &clap::Command,
// arg: Option<&clap::Arg>,
// value: &std::ffi::OsStr,
// ) -> Result<Self::Value, clap::Error> {
// let value = value.to_string_lossy().to_string();

// match value.split_once('=') {
// Some(("query", query)) => Ok(T::try_from_raw_query(query).or({
// // query: PythonQuery::Custom(query.try_into().or({
// let mut err = clap::Error::new(ErrorKind::ValueValidation).with_cmd(cmd);

// err.insert(
// ContextKind::InvalidArg,
// ContextValue::String("query".to_string()),
// );

// err.insert(
// ContextKind::InvalidValue,
// ContextValue::String(query.to_string()),
// );

// Err(err)
// })?),
// // }),
// _ => {
// // let premade: PremadePythonQuery = value.as_str().try_into().or({
// let premade = T::try_from_raw_premade_query(value.as_str())
// // .try_into()
// .or({
// let mut err =
// clap::Error::new(ErrorKind::ValueValidation).with_cmd(cmd);

// if let Some(arg) = arg {
// err.insert(
// ContextKind::InvalidArg,
// ContextValue::String(arg.to_string()),
// );
// }

// err.insert(
// ContextKind::InvalidValue,
// ContextValue::String(value.to_string()),
// );

// err.insert(
// ContextKind::SuggestedValue,
// ContextValue::Strings(
// self.possible_values()
// .expect("Possible values to be defined")
// .map(|s| s.get_name().to_owned())
// .collect_vec(),
// ),
// );

// Err(err)
// })?;

// Ok(premade)
// // Ok(Python {
// // query: PythonQuery::Premade(premade),
// // })
// }
// }
// }

// fn possible_values(
// &self,
// ) -> Option<Box<dyn Iterator<Item = clap::builder::PossibleValue> + '_>> {
// Some(Box::new({
// let premades = all::<PremadePythonQuery>().collect::<Vec<_>>();

// premades
// .into_iter()
// .map(|p| p.to_string())
// .chain(vec!["query=<S EXPRESSION>".to_string()])
// .map(clap::builder::PossibleValue::new)
// }))
// }
// }

#[cfg(feature = "german")]
#[derive(Parser, Debug)]
#[group(required = false, multiple = true)]
Expand Down
56 changes: 5 additions & 51 deletions src/scoping/langs/python.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,22 @@
use super::{
LanguageScopedViewBuildStep, LanguageScoperError, TSLanguage, TSParser, TSQuery, TSQueryCursor,
};
use super::{LanguageScopedViewBuildStep, TSLanguage, TSParser, TSQuery, TSQueryCursor};
use crate::scoping::{ScopedViewBuildStep, ScopedViewBuilder};
use clap::{Parser, ValueEnum};
use enum_iterator::Sequence;
use clap::ValueEnum;
use std::fmt::Debug;
use strum::{Display, EnumString};
use strum::Display;
use tree_sitter::QueryError;

#[derive(Debug, Clone)]
pub struct Python {
pub query: PythonQuery,
}

// pub trait FromRawQuery {
// fn try_from_raw_query(query: &str) -> Result<Self, LanguageScoperError>
// where
// Self: Sized;

// fn try_from_raw_premade_query(query: &str) -> Result<Self, LanguageScoperError>
// where
// Self: Sized;
// }

// impl FromRawQuery for Python {
// fn try_from_raw_query(
// query: &str,
// // query: impl TryInto<MyBullshitQuery, Error = LanguageScoperError>,
// ) -> Result<Self, LanguageScoperError> {
// Ok(Self {
// query: PythonQuery::Custom(query.try_into()?),
// })
// }

// fn try_from_raw_premade_query(query: &str) -> Result<Self, LanguageScoperError> {
// let x = query
// .try_into()
// .map_err(|_| LanguageScoperError::NoSuchPremadeQuery(query.to_string()))?;

// Ok(Self {
// query: PythonQuery::Premade(x),
// })
// }
// }

#[derive(Debug, Display, Clone)]
pub enum PythonQuery {
Custom(CustomPythonQuery),
Premade(PremadePythonQuery),
}

#[derive(Debug, Clone, Copy, EnumString, Display, Sequence, ValueEnum)]
#[strum(serialize_all = "kebab-case")]
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum PremadePythonQuery {
Comments,
DocStrings,
Expand All @@ -76,20 +41,9 @@ impl From<&PremadePythonQuery> for TSQuery {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CustomPythonQuery(String);

// impl TryFrom<&str> for CustomPythonQuery {
// type Error = LanguageScoperError;

// fn try_from(value: &str) -> Result<Self, Self::Error> {
// match TSQuery::new(tree_sitter_python::language(), value) {
// Ok(_) => Ok(Self(value.to_string())),
// Err(e) => Err(e.into()),
// }
// }
// }

impl TryFrom<String> for CustomPythonQuery {
type Error = QueryError;

Expand Down

0 comments on commit a2135a2

Please sign in to comment.