Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Apr 11, 2024
1 parent fa321ed commit 4b06edd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
3 changes: 1 addition & 2 deletions vlib/v/doc/doc.v
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,7 @@ pub fn (mut d Doc) file_asts(mut file_asts []ast.File) ! {
}
if d.contents[name].kind != .none_ || node.kind == .none_ {
d.contents[name].children << node.children
d.contents[name].children.sort_by_name()
d.contents[name].children.sort_by_kind()
d.contents[name].children.arrange()
}
}
}
Expand Down
52 changes: 25 additions & 27 deletions vlib/v/doc/node.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,39 @@ pub fn (nodes []DocNode) find(symname string) !DocNode {
return error('symbol not found')
}

// sort_by_name sorts the array based on the symbol names.
pub fn (mut nodes []DocNode) sort_by_name() {
if doc.should_sort {
nodes.sort_with_compare(compare_nodes_by_name)
// arrange sorts the DocNodes based on their symbols and names.
pub fn (mut nodes []DocNode) arrange() {
if !doc.should_sort {
return
}
}

// sort_by_kind sorts the array based on the symbol kind.
pub fn (mut nodes []DocNode) sort_by_kind() {
if doc.should_sort {
nodes.sort_with_compare(compare_nodes_by_kind)
mut kinds := []SymbolKind{}
for v in nodes {
if v.kind !in kinds {
kinds << v.kind
}
}
}

fn compare_nodes_by_kind(a &DocNode, b &DocNode) int {
ak := int(a.kind)
bk := int(b.kind)
return match true {
ak < bk { -1 }
ak > bk { 1 }
else { 0 }
kinds.sort_with_compare(fn (a &SymbolKind, b &SymbolKind) int {
ak := int(*a)
bk := int(*b)
return match true {
ak < bk { -1 }
ak > bk { 1 }
else { 0 }
}
})
mut res := []DocNode{}
for k in kinds {
mut kind_nodes := nodes.filter(it.kind == k)
kind_nodes.sort(a.name < b.name)
res << kind_nodes
}
}

fn compare_nodes_by_name(a &DocNode, b &DocNode) int {
al := a.name.to_lower()
bl := b.name.to_lower()
return compare_strings(al, bl)
nodes = res.clone()
}

// arr() converts the map into an array of `DocNode`.
pub fn (cnts map[string]DocNode) arr() []DocNode {
mut contents := cnts.values()
contents.sort_by_name()
contents.sort_by_kind()
contents.arrange()
return contents
}

Expand Down

0 comments on commit 4b06edd

Please sign in to comment.