3 # Functions for dealing with type hierarchy (super types/sub types)
8 # Parse the type hierarchy in 'typeHier' and displays a tree of type names
9 # in the current buffer. This function is called recursively to display the
10 # super/sub type hierarchy.
12 # Returns the line number where the next type name should be added.
13 def TypeTreeGenerate(isSuper: bool, typeHier: dict<any>, pfx_arg: string,
14 typeTree: list<string>, typeUriMap: list<dict<any>>)
16 var itemHasChildren = false
18 if typeHier->has_key('parents') && !typeHier.parents->empty()
19 itemHasChildren = true
22 if typeHier->has_key('children') && !typeHier.children->empty()
23 itemHasChildren = true
27 var itemBranchPfx: string
31 itemBranchPfx = pfx_arg->empty() ? '' : ' '
35 var kindstr = symbol.SymbolKindToName(typeHier.kind)
37 typestr = $'{pfx_arg}{itemBranchPfx}{typeHier.name} ({kindstr[0]})'
39 typestr = $'{pfx_arg}{itemBranchPfx}{typeHier.name}'
41 typeTree->add(typestr)
42 typeUriMap->add(typeHier)
44 # last item to process
49 var items: list<dict<any>>
50 items = isSuper ? typeHier.parents : typeHier.children
53 TypeTreeGenerate(isSuper, item, $'{pfx_arg}| ', typeTree, typeUriMap)
57 # Display a popup with the file containing a type and highlight the line and
59 def UpdateTypeHierFileInPopup(lspserver: dict<any>, typeUriMap: list<dict<any>>)
60 if lspserver.typeHierPopup->winbufnr() == -1
64 lspserver.typeHierFilePopup->popup_close()
66 var n = line('.', lspserver.typeHierPopup) - 1
67 var fname: string = util.LspUriToFile(typeUriMap[n].uri)
69 var bnr = fname->bufadd()
75 title: $"{fname->fnamemodify(':t')} ({fname->fnamemodify(':h')})",
80 minwidth: winwidth(0) - 38,
81 maxwidth: winwidth(0) - 38,
87 lspserver.typeHierFilePopup = popup_create(bnr, popupAttrs)
88 var cmds =<< trim eval END
89 [{typeUriMap[n].range.start.line + 1}, 1]->cursor()
92 win_execute(lspserver.typeHierFilePopup, cmds)
94 lspserver.typeHierFilePopup->clearmatches()
95 var start_col = util.GetLineByteFromPos(bnr,
96 typeUriMap[n].selectionRange.start) + 1
97 var end_col = util.GetLineByteFromPos(bnr, typeUriMap[n].selectionRange.end)
98 var pos = [[typeUriMap[n].selectionRange.start.line + 1,
99 start_col, end_col - start_col + 1]]
100 matchaddpos('Search', pos, 10, -1, {window: lspserver.typeHierFilePopup})
103 def TypeHierPopupFilter(lspserver: dict<any>, typeUriMap: list<dict<any>>,
104 popupID: number, key: string): bool
105 popupID->popup_filter_menu(key)
106 if lspserver.typeHierPopup->winbufnr() == -1
108 if lspserver.typeHierFilePopup->winbufnr() != -1
109 lspserver.typeHierFilePopup->popup_close()
111 lspserver.typeHierFilePopup = -1
112 lspserver.typeHierPopup = -1
114 UpdateTypeHierFileInPopup(lspserver, typeUriMap)
120 def TypeHierPopupCallback(lspserver: dict<any>, typeUriMap: list<dict<any>>,
121 popupID: number, selIdx: number)
122 if lspserver.typeHierFilePopup->winbufnr() != -1
123 lspserver.typeHierFilePopup->popup_close()
125 lspserver.typeHierFilePopup = -1
126 lspserver.typeHierPopup = -1
133 # Save the current cursor location in the tag stack.
134 util.PushCursorToTagStack()
135 util.JumpToLspLocation(typeUriMap[selIdx - 1], '')
138 # Show the super or sub type hierarchy items 'types' as a tree in a popup window
139 export def ShowTypeHierarchy(lspserver: dict<any>, isSuper: bool, types: dict<any>)
141 if lspserver.typeHierPopup->winbufnr() != -1
142 # If the type hierarchy popup window is already present, close it.
143 lspserver.typeHierPopup->popup_close()
146 var typeTree: list<string>
147 var typeUriMap: list<dict<any>>
149 # Generate a tree of the type hierarchy items
150 TypeTreeGenerate(isSuper, types, '', typeTree, typeUriMap)
152 # Display a popup window with the type hierarchy tree and a popup window for
155 title: $'{isSuper ? "Super" : "Sub"}Type Hierarchy',
159 col: winwidth(0) - 34,
166 filter: function(TypeHierPopupFilter, [lspserver, typeUriMap]),
167 callback: function(TypeHierPopupCallback, [lspserver, typeUriMap])
169 lspserver.typeHierPopup = popup_menu(typeTree, popupAttrs)
170 UpdateTypeHierFileInPopup(lspserver, typeUriMap)
173 # vim: tabstop=8 shiftwidth=2 softtabstop=2