mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-11-23 12:46:43 +00:00
refactor: rename local variables (cont.) (#1026)
This commit is contained in:
parent
9017e05afa
commit
de8e45a539
50 changed files with 837 additions and 814 deletions
|
|
@ -106,23 +106,23 @@ impl fmt::Display for SigHoverDocs<'_> {
|
|||
|
||||
fn write_param_docs(
|
||||
f: &mut fmt::Formatter<'_>,
|
||||
p: &ParamDocsT<TypeRepr>,
|
||||
docs: &ParamDocsT<TypeRepr>,
|
||||
kind: &str,
|
||||
is_first: &mut bool,
|
||||
) -> fmt::Result {
|
||||
if *is_first {
|
||||
*is_first = false;
|
||||
write!(f, "\n\n## {}\n\n", p.name)?;
|
||||
write!(f, "\n\n## {}\n\n", docs.name)?;
|
||||
} else {
|
||||
write!(f, "\n\n## {} ({kind})\n\n", p.name)?;
|
||||
write!(f, "\n\n## {} ({kind})\n\n", docs.name)?;
|
||||
}
|
||||
|
||||
// p.cano_type.0
|
||||
if let Some(t) = &p.cano_type {
|
||||
if let Some(t) = &docs.cano_type {
|
||||
write!(f, "```typc\ntype: {}\n```\n\n", t.2)?;
|
||||
}
|
||||
|
||||
f.write_str(p.docs.trim())?;
|
||||
f.write_str(docs.docs.trim())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -131,8 +131,8 @@ impl fmt::Display for SigHoverDocs<'_> {
|
|||
f.write_str("\n\n# Positional Parameters")?;
|
||||
|
||||
let mut is_first = true;
|
||||
for p in &docs.pos {
|
||||
write_param_docs(f, p, "positional", &mut is_first)?;
|
||||
for pos_docs in &docs.pos {
|
||||
write_param_docs(f, pos_docs, "positional", &mut is_first)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -149,8 +149,8 @@ impl fmt::Display for SigHoverDocs<'_> {
|
|||
f.write_str("\n\n# Named Parameters")?;
|
||||
|
||||
let mut is_first = true;
|
||||
for p in docs.named.values() {
|
||||
write_param_docs(f, p, "named", &mut is_first)?;
|
||||
for named_docs in docs.named.values() {
|
||||
write_param_docs(f, named_docs, "named", &mut is_first)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -176,10 +176,10 @@ impl SignatureDocs {
|
|||
};
|
||||
|
||||
f.write_char('(')?;
|
||||
for p in &self.pos {
|
||||
for pos_docs in &self.pos {
|
||||
write_sep(f)?;
|
||||
f.write_str(&p.name)?;
|
||||
if let Some(t) = &p.cano_type {
|
||||
f.write_str(&pos_docs.name)?;
|
||||
if let Some(t) = &pos_docs.cano_type {
|
||||
write!(f, ": {}", t.0)?;
|
||||
}
|
||||
}
|
||||
|
|
@ -199,27 +199,27 @@ impl SignatureDocs {
|
|||
name_prints.push((v.name.clone(), ty, v.default.clone()))
|
||||
}
|
||||
name_prints.sort();
|
||||
for (k, t, v) in name_prints {
|
||||
for (name, ty, val) in name_prints {
|
||||
write_sep(f)?;
|
||||
let v = v.as_deref().unwrap_or("any");
|
||||
let mut v = v.trim();
|
||||
if v.starts_with('{') && v.ends_with('}') && v.len() > 30 {
|
||||
v = "{ .. }"
|
||||
let val = val.as_deref().unwrap_or("any");
|
||||
let mut default = val.trim();
|
||||
if default.starts_with('{') && default.ends_with('}') && default.len() > 30 {
|
||||
default = "{ .. }"
|
||||
}
|
||||
if v.starts_with('`') && v.ends_with('`') && v.len() > 30 {
|
||||
v = "raw"
|
||||
if default.starts_with('`') && default.ends_with('`') && default.len() > 30 {
|
||||
default = "raw"
|
||||
}
|
||||
if v.starts_with('[') && v.ends_with(']') && v.len() > 30 {
|
||||
v = "content"
|
||||
if default.starts_with('[') && default.ends_with(']') && default.len() > 30 {
|
||||
default = "content"
|
||||
}
|
||||
f.write_str(&k)?;
|
||||
if let Some(t) = t {
|
||||
write!(f, ": {t}")?;
|
||||
f.write_str(&name)?;
|
||||
if let Some(ty) = ty {
|
||||
write!(f, ": {ty}")?;
|
||||
}
|
||||
if v.contains('\n') {
|
||||
write!(f, " = {}", v.replace("\n", "\n "))?;
|
||||
if default.contains('\n') {
|
||||
write!(f, " = {}", default.replace("\n", "\n "))?;
|
||||
} else {
|
||||
write!(f, " = {v}")?;
|
||||
write!(f, " = {default}")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -349,12 +349,12 @@ pub(crate) fn sig_docs(sig: &Signature) -> Option<SignatureDocs> {
|
|||
.pos()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, pos)| (pos, type_sig.pos(i)));
|
||||
.map(|(idx, pos)| (pos, type_sig.pos(idx)));
|
||||
let named_in = sig
|
||||
.primary()
|
||||
.named()
|
||||
.iter()
|
||||
.map(|x| (x, type_sig.named(&x.name)));
|
||||
.map(|param| (param, type_sig.named(¶m.name)));
|
||||
let rest_in = sig.primary().rest().map(|x| (x, type_sig.rest_param()));
|
||||
|
||||
let ret_in = type_sig.body.as_ref();
|
||||
|
|
|
|||
|
|
@ -15,10 +15,10 @@ pub use module::*;
|
|||
pub use package::*;
|
||||
pub(crate) use tidy::*;
|
||||
|
||||
fn file_id_repr(k: FileId) -> String {
|
||||
if let Some(p) = k.package() {
|
||||
format!("{p}{}", unix_slash(k.vpath().as_rooted_path()))
|
||||
fn file_id_repr(fid: FileId) -> String {
|
||||
if let Some(spec) = fid.package() {
|
||||
format!("{spec}{}", unix_slash(fid.vpath().as_rooted_path()))
|
||||
} else {
|
||||
unix_slash(k.vpath().as_rooted_path())
|
||||
unix_slash(fid.vpath().as_rooted_path())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,9 +48,9 @@ pub fn module_docs(ctx: &mut LocalContext, entry_point: FileId) -> StrResult<Pac
|
|||
|
||||
let module_uses = aliases
|
||||
.into_iter()
|
||||
.map(|(k, mut v)| {
|
||||
.map(|(fid, mut v)| {
|
||||
v.sort_by(|a, b| a.len().cmp(&b.len()).then(a.cmp(b)));
|
||||
(file_id_repr(k), v.into())
|
||||
(file_id_repr(fid), v.into())
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
|
@ -113,7 +113,7 @@ struct ScanDefCtx<'a> {
|
|||
}
|
||||
|
||||
impl ScanDefCtx<'_> {
|
||||
fn defs(&mut self, path: EcoVec<&str>, ei: Arc<ExprInfo>) -> DefInfo {
|
||||
fn defs(&mut self, paths: EcoVec<&str>, ei: Arc<ExprInfo>) -> DefInfo {
|
||||
let name = {
|
||||
let stem = ei.fid.vpath().as_rooted_path().file_stem();
|
||||
stem.and_then(|s| Some(Interned::new_str(s.to_str()?)))
|
||||
|
|
@ -121,8 +121,8 @@ impl ScanDefCtx<'_> {
|
|||
};
|
||||
let module_decl = Decl::module(name.clone(), ei.fid).into();
|
||||
let site = Some(self.root);
|
||||
let p = path.clone();
|
||||
self.def(&name, p, site.as_ref(), &module_decl, None)
|
||||
let paths = paths.clone();
|
||||
self.def(&name, paths, site.as_ref(), &module_decl, None)
|
||||
}
|
||||
|
||||
fn expr(
|
||||
|
|
@ -133,7 +133,7 @@ impl ScanDefCtx<'_> {
|
|||
val: &Expr,
|
||||
) -> DefInfo {
|
||||
match val {
|
||||
Expr::Decl(d) => self.def(key, path, site, d, Some(val)),
|
||||
Expr::Decl(decl) => self.def(key, path, site, decl, Some(val)),
|
||||
Expr::Ref(r) if r.root.is_some() => {
|
||||
self.expr(key, path, site, r.root.as_ref().unwrap())
|
||||
}
|
||||
|
|
@ -170,7 +170,7 @@ impl ScanDefCtx<'_> {
|
|||
) -> DefInfo {
|
||||
let def = self.ctx.def_of_decl(decl);
|
||||
let def_docs = def.and_then(|def| self.ctx.def_docs(&def));
|
||||
let docs = def_docs.as_ref().map(|d| d.docs().clone());
|
||||
let docs = def_docs.as_ref().map(|docs| docs.docs().clone());
|
||||
let children = match decl.as_ref() {
|
||||
Decl::Module(..) => decl.file_id().and_then(|fid| {
|
||||
// only generate docs for the same package
|
||||
|
|
@ -195,10 +195,10 @@ impl ScanDefCtx<'_> {
|
|||
let symbols = ei
|
||||
.exports
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
.map(|(name, val)| {
|
||||
let mut path = path.clone();
|
||||
path.push(k);
|
||||
self.expr(k, path.clone(), Some(&fid), v)
|
||||
path.push(name);
|
||||
self.expr(name, path.clone(), Some(&fid), val)
|
||||
})
|
||||
.collect();
|
||||
Some(symbols)
|
||||
|
|
@ -220,7 +220,8 @@ impl ScanDefCtx<'_> {
|
|||
oneliner: None,
|
||||
};
|
||||
|
||||
if let Some((span, mod_fid)) = head.decl.as_ref().and_then(|d| d.file_id()).zip(site) {
|
||||
if let Some((span, mod_fid)) = head.decl.as_ref().and_then(|decl| decl.file_id()).zip(site)
|
||||
{
|
||||
if span != *mod_fid {
|
||||
head.is_external = true;
|
||||
head.oneliner = head.docs.map(|docs| oneliner(&docs).to_owned());
|
||||
|
|
@ -229,7 +230,7 @@ impl ScanDefCtx<'_> {
|
|||
}
|
||||
|
||||
// Insert module that is not exported
|
||||
if let Some(fid) = head.decl.as_ref().and_then(|d| d.file_id()) {
|
||||
if let Some(fid) = head.decl.as_ref().and_then(|del| del.file_id()) {
|
||||
// only generate docs for the same package
|
||||
if fid.package() == self.for_spec {
|
||||
let av = self.aliases.entry(fid).or_default();
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ pub fn package_docs(ctx: &mut LocalContext, spec: &PackageInfo) -> StrResult<Str
|
|||
|
||||
crate::log_debug_ct!("module: {primary} -- {parent_ident}");
|
||||
|
||||
let persist_fid = fid.map(|f| file_ids.insert_full(f).0);
|
||||
let persist_fid = fid.map(|fid| file_ids.insert_full(fid).0);
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ModuleInfo {
|
||||
|
|
@ -98,17 +98,17 @@ pub fn package_docs(ctx: &mut LocalContext, spec: &PackageInfo) -> StrResult<Str
|
|||
parent_ident: EcoString,
|
||||
aka: EcoVec<String>,
|
||||
}
|
||||
let m = jbase64(&ModuleInfo {
|
||||
let module_info = jbase64(&ModuleInfo {
|
||||
prefix: primary.as_str().into(),
|
||||
name: def.name.clone(),
|
||||
loc: persist_fid,
|
||||
parent_ident: parent_ident.clone(),
|
||||
aka,
|
||||
});
|
||||
let _ = writeln!(md, "<!-- begin:module {primary} {m} -->");
|
||||
let _ = writeln!(md, "<!-- begin:module {primary} {module_info} -->");
|
||||
|
||||
for mut child in children {
|
||||
let span = child.decl.as_ref().map(|d| d.span());
|
||||
let span = child.decl.as_ref().map(|decl| decl.span());
|
||||
let fid_range = span.and_then(|v| {
|
||||
v.id().and_then(|fid| {
|
||||
let allocated = file_ids.insert_full(fid).0;
|
||||
|
|
@ -117,7 +117,7 @@ pub fn package_docs(ctx: &mut LocalContext, spec: &PackageInfo) -> StrResult<Str
|
|||
Some((allocated, rng.start, rng.end))
|
||||
})
|
||||
});
|
||||
let child_fid = child.decl.as_ref().and_then(|d| d.file_id());
|
||||
let child_fid = child.decl.as_ref().and_then(|decl| decl.file_id());
|
||||
let child_fid = child_fid.or_else(|| span.and_then(Span::id)).or(fid);
|
||||
let span = fid_range.or_else(|| {
|
||||
let fid = child_fid?;
|
||||
|
|
@ -191,8 +191,12 @@ pub fn package_docs(ctx: &mut LocalContext, spec: &PackageInfo) -> StrResult<Str
|
|||
(Some(docs), _) if !child.is_external => {
|
||||
let _ = writeln!(md, "{}", remove_list_annotations(docs.docs()));
|
||||
printed_docs = true;
|
||||
if let DefDocs::Function(f) = docs {
|
||||
for param in f.pos.iter().chain(f.named.values()).chain(f.rest.as_ref())
|
||||
if let DefDocs::Function(docs) = docs {
|
||||
for param in docs
|
||||
.pos
|
||||
.iter()
|
||||
.chain(docs.named.values())
|
||||
.chain(docs.rest.as_ref())
|
||||
{
|
||||
let _ = writeln!(md, "<!-- begin:param {} -->", param.name);
|
||||
let ty = match ¶m.cano_type {
|
||||
|
|
@ -350,21 +354,21 @@ mod tests {
|
|||
use crate::tests::*;
|
||||
|
||||
fn test(pkg: PackageSpec) {
|
||||
run_with_sources("", |verse: &mut LspUniverse, p| {
|
||||
let path = verse.registry.resolve(&pkg).unwrap();
|
||||
run_with_sources("", |verse: &mut LspUniverse, path| {
|
||||
let pkg_root = verse.registry.resolve(&pkg).unwrap();
|
||||
let pi = PackageInfo {
|
||||
path: path.as_ref().to_owned(),
|
||||
path: pkg_root.as_ref().to_owned(),
|
||||
namespace: pkg.namespace,
|
||||
name: pkg.name,
|
||||
version: pkg.version.to_string(),
|
||||
};
|
||||
run_with_ctx(verse, p, &|a, _p| {
|
||||
let d = package_docs(a, &pi).unwrap();
|
||||
run_with_ctx(verse, path, &|a, _p| {
|
||||
let docs = package_docs(a, &pi).unwrap();
|
||||
let dest = format!(
|
||||
"../../target/{}-{}-{}.md",
|
||||
pi.namespace, pi.name, pi.version
|
||||
);
|
||||
std::fs::write(dest, d).unwrap();
|
||||
std::fs::write(dest, docs).unwrap();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,14 +32,14 @@ pub fn identify_pat_docs(converted: &str) -> StrResult<TidyPatDocs> {
|
|||
let mut return_ty = None;
|
||||
let mut break_line = None;
|
||||
|
||||
let mut i = lines.len();
|
||||
let mut line_width = lines.len();
|
||||
'search: loop {
|
||||
if i == 0 {
|
||||
if line_width == 0 {
|
||||
break;
|
||||
}
|
||||
i -= 1;
|
||||
line_width -= 1;
|
||||
|
||||
let line = lines[i];
|
||||
let line = lines[line_width];
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
|
@ -52,7 +52,7 @@ pub fn identify_pat_docs(converted: &str) -> StrResult<TidyPatDocs> {
|
|||
continue;
|
||||
};
|
||||
|
||||
break_line = Some(i);
|
||||
break_line = Some(line_width);
|
||||
return_ty = Some(w.trim().into());
|
||||
break;
|
||||
}
|
||||
|
|
@ -61,10 +61,10 @@ pub fn identify_pat_docs(converted: &str) -> StrResult<TidyPatDocs> {
|
|||
.trim_end()
|
||||
.strip_suffix("<!-- typlite:end:list-item 0 -->")
|
||||
else {
|
||||
break_line = Some(i + 1);
|
||||
break_line = Some(line_width + 1);
|
||||
break 'search;
|
||||
};
|
||||
let mut current_line_no = i;
|
||||
let mut current_line_no = line_width;
|
||||
|
||||
loop {
|
||||
// <!-- typlite:begin:list-item -->
|
||||
|
|
@ -84,7 +84,7 @@ pub fn identify_pat_docs(converted: &str) -> StrResult<TidyPatDocs> {
|
|||
buf.push(line_content);
|
||||
|
||||
if current_line_no == 0 {
|
||||
break_line = Some(i + 1);
|
||||
break_line = Some(line_width + 1);
|
||||
break 'search;
|
||||
}
|
||||
current_line_no -= 1;
|
||||
|
|
@ -95,7 +95,7 @@ pub fn identify_pat_docs(converted: &str) -> StrResult<TidyPatDocs> {
|
|||
buf.reverse();
|
||||
|
||||
let Some(first_line) = buf.first_mut() else {
|
||||
break_line = Some(i + 1);
|
||||
break_line = Some(line_width + 1);
|
||||
break 'search;
|
||||
};
|
||||
*first_line = first_line.trim();
|
||||
|
|
@ -107,11 +107,11 @@ pub fn identify_pat_docs(converted: &str) -> StrResult<TidyPatDocs> {
|
|||
*first_line = rest.trim();
|
||||
Some((param_name.into(), type_content.into()))
|
||||
}) else {
|
||||
break_line = Some(i + 1);
|
||||
break_line = Some(line_width + 1);
|
||||
break 'search;
|
||||
};
|
||||
|
||||
i = current_line_no;
|
||||
line_width = current_line_no;
|
||||
params.push(TidyParamDocs {
|
||||
name: param_line.0,
|
||||
types: param_line.1,
|
||||
|
|
@ -143,15 +143,15 @@ pub fn identify_tidy_module_docs(docs: EcoString) -> StrResult<TidyModuleDocs> {
|
|||
fn match_brace(trim_start: &str) -> Option<(&str, &str)> {
|
||||
let mut brace_count = 1;
|
||||
let mut end = 0;
|
||||
for (i, c) in trim_start.char_indices() {
|
||||
match c {
|
||||
for (idx, ch) in trim_start.char_indices() {
|
||||
match ch {
|
||||
'(' => brace_count += 1,
|
||||
')' => brace_count -= 1,
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if brace_count == 0 {
|
||||
end = i;
|
||||
end = idx;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -171,9 +171,9 @@ mod tests {
|
|||
use super::TidyParamDocs;
|
||||
|
||||
fn func(s: &str) -> String {
|
||||
let f = super::identify_pat_docs(s).unwrap();
|
||||
let mut res = format!(">> docs:\n{}\n<< docs", f.docs);
|
||||
if let Some(t) = f.return_ty {
|
||||
let docs = super::identify_pat_docs(s).unwrap();
|
||||
let mut res = format!(">> docs:\n{}\n<< docs", docs.docs);
|
||||
if let Some(t) = docs.return_ty {
|
||||
res.push_str(&format!("\n>>return\n{t}\n<<return"));
|
||||
}
|
||||
for TidyParamDocs {
|
||||
|
|
@ -181,7 +181,7 @@ mod tests {
|
|||
types,
|
||||
docs,
|
||||
default: _,
|
||||
} in f.params
|
||||
} in docs.params
|
||||
{
|
||||
let _ = write!(res, "\n>>arg {name}: {types}\n{docs}\n<< arg");
|
||||
}
|
||||
|
|
@ -189,9 +189,9 @@ mod tests {
|
|||
}
|
||||
|
||||
fn var(s: &str) -> String {
|
||||
let f = super::identify_pat_docs(s).unwrap();
|
||||
let mut res = format!(">> docs:\n{}\n<< docs", f.docs);
|
||||
if let Some(t) = f.return_ty {
|
||||
let docs = super::identify_pat_docs(s).unwrap();
|
||||
let mut res = format!(">> docs:\n{}\n<< docs", docs.docs);
|
||||
if let Some(t) = docs.return_ty {
|
||||
res.push_str(&format!("\n>>return\n{t}\n<<return"));
|
||||
}
|
||||
res
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue