@@ -5,7 +5,8 @@ function win.new(config)
5
5
6
6
self .id = nil
7
7
self .config = {
8
- width = config .width or 40 ,
8
+ min_width = config .min_width or 30 ,
9
+ max_width = config .max_width or 60 ,
9
10
max_height = config .max_height or 10 ,
10
11
cursorline = config .cursorline or false ,
11
12
wrap = config .wrap or false ,
@@ -21,8 +22,8 @@ function win:get_buf()
21
22
-- create buffer if it doesn't exist
22
23
if self .buf == nil or not vim .api .nvim_buf_is_valid (self .buf ) then
23
24
self .buf = vim .api .nvim_create_buf (false , true )
24
- -- vim.api.nvim_buf_set_option(self.buf, 'tabstop', 1) -- prevents tab widths from being unpredictable
25
- -- vim.api.nvim_buf_set_option(self.buf, 'filetype', self.config.filetype)
25
+ vim .api .nvim_set_option_value ( ' tabstop' , 1 , { buf = self . buf } ) -- prevents tab widths from being unpredictable
26
+ vim .api .nvim_set_option_value ( ' filetype' , self .config .filetype , { buf = self . buf } )
26
27
end
27
28
return self .buf
28
29
end
@@ -43,7 +44,7 @@ function win:open()
43
44
self .id = vim .api .nvim_open_win (self :get_buf (), false , {
44
45
relative = ' cursor' ,
45
46
style = ' minimal' ,
46
- width = self .config .width ,
47
+ width = self .config .min_width ,
47
48
height = self .config .max_height ,
48
49
row = 1 ,
49
50
col = 1 ,
@@ -80,11 +81,14 @@ function win:update_position(relative_to)
80
81
local cursor_row = cursor [1 ]
81
82
local cursor_col = cursor [2 ]
82
83
83
- -- set height to current line count
84
+ -- set width to current content width, bounded by min and max
85
+ local width = math.max (math.min (self :get_content_width (), config .max_width ), config .min_width )
86
+ vim .api .nvim_win_set_width (winnr , width )
87
+
88
+ -- set height to current line count, bounded by max
84
89
local height = math.min (self :get_content_height (), config .max_height )
85
90
vim .api .nvim_win_set_height (winnr , height )
86
91
87
- -- relative to cursor
88
92
if relative_to == ' cursor' then
89
93
local is_space_below = screen_height - cursor_row > height
90
94
@@ -102,9 +106,7 @@ function win:update_position(relative_to)
102
106
local max_width_right = screen_width - cursor_col - relative_win_config .width - 7
103
107
local max_width_left = cursor_col
104
108
105
- local width = math.min (math.max (max_width_left , max_width_right ), config .width )
106
-
107
- if max_width_right >= config .width or max_width_right >= max_width_left then
109
+ if max_width_right >= width or max_width_right >= max_width_left then
108
110
vim .api .nvim_win_set_config (winnr , {
109
111
relative = ' win' ,
110
112
win = relative_to ,
@@ -121,7 +123,6 @@ function win:update_position(relative_to)
121
123
width = width ,
122
124
})
123
125
end
124
- -- No idea what it's supposed to be relative to
125
126
else
126
127
error (' Invalid relative config' )
127
128
end
@@ -131,15 +132,15 @@ end
131
132
function win :get_content_height ()
132
133
if not self :is_open () then return 0 end
133
134
return vim .api .nvim_win_text_height (self :get_win (), {}).all
134
- --
135
- -- if not self.config.wrap then return vim.api.nvim_buf_line_count(self.buf) end
136
- -- local height = 0
137
- -- vim.api.nvim_buf_call(self.buf, function()
138
- -- for _, text in ipairs(vim.api.nvim_buf_get_lines(self.buf, 0, -1, false)) do
139
- -- height = height + math.max(1, math.ceil( vim.fn.strdisplaywidth(text) / self.config.width))
140
- -- end
141
- -- end)
142
- -- return height
135
+ end
136
+
137
+ function win : get_content_width ()
138
+ if not self : is_open () then return 0 end
139
+ local max_width = 0
140
+ for _ , line in ipairs ( vim .api . nvim_buf_get_lines ( self .buf , 0 , - 1 , false )) do
141
+ max_width = math.max ( max_width , vim . api . nvim_strwidth ( line ))
142
+ end
143
+ return max_width
143
144
end
144
145
145
146
return win
0 commit comments