setFuncImpl is the underlying implementation of [FuncButton.SetFunc]. It should typically not be used by end-user code.
(gfun *types.Func, rfun reflect.Value)
| 239 | // setFuncImpl is the underlying implementation of [FuncButton.SetFunc]. |
| 240 | // It should typically not be used by end-user code. |
| 241 | func (fb *FuncButton) setFuncImpl(gfun *types.Func, rfun reflect.Value) *FuncButton { |
| 242 | fb.typesFunc = gfun |
| 243 | fb.reflectFunc = rfun |
| 244 | fb.setArgs() |
| 245 | fb.setReturns() |
| 246 | snm := fb.typesFunc.Name |
| 247 | // get name without package |
| 248 | li := strings.LastIndex(snm, ".") |
| 249 | isAnonymous := isAnonymousFunction(snm) |
| 250 | if snm == "reflect.makeFuncStub" { // used for anonymous functions in yaegi |
| 251 | snm = "Anonymous function" |
| 252 | li = -1 |
| 253 | } else if isAnonymous { |
| 254 | snm = strings.TrimRightFunc(snm, func(r rune) bool { |
| 255 | return unicode.IsDigit(r) || r == '.' |
| 256 | }) |
| 257 | snm = strings.TrimSuffix(snm, ".func") |
| 258 | // we cut at the second to last period (we want to keep the |
| 259 | // receiver / package for anonymous functions) |
| 260 | li = strings.LastIndex(snm[:strings.LastIndex(snm, ".")], ".") |
| 261 | } |
| 262 | if li >= 0 { |
| 263 | snm = snm[li+1:] // must also get rid of "." |
| 264 | // if we are a global function, we may have gone too far with the second to last period, |
| 265 | // so we go after the last slash if there still is one |
| 266 | if strings.Contains(snm, "/") { |
| 267 | snm = snm[strings.LastIndex(snm, "/")+1:] |
| 268 | } |
| 269 | } |
| 270 | snm = strings.Map(func(r rune) rune { |
| 271 | if r == '(' || r == ')' || r == '*' { |
| 272 | return -1 |
| 273 | } |
| 274 | return r |
| 275 | }, snm) |
| 276 | txt := strcase.ToSentence(snm) |
| 277 | fb.SetText(txt) |
| 278 | // doc formatting interferes with anonymous functions |
| 279 | if !isAnonymous { |
| 280 | fb.typesFunc.Doc = types.FormatDoc(fb.typesFunc.Doc, snm, txt) |
| 281 | } |
| 282 | fb.SetTooltip(fb.typesFunc.Doc) |
| 283 | return fb |
| 284 | } |
| 285 | |
| 286 | func (fb *FuncButton) goodContext() Widget { |
| 287 | ctx := fb.Context |
no test coverage detected