summaryrefslogtreecommitdiff
path: root/internal/lsp/handlers.go
blob: 63b32d0a105e5f97b4fa203d83d97fd4601adc89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
// Summary: LSP JSON-RPC handlers; implements core methods and integrates with the LLM client when enabled.
// Not yet reviewed by a human
package lsp

import (
	"context"
	"encoding/json"
	"fmt"
	"hexai/internal"
	"hexai/internal/llm"
	"hexai/internal/logging"
	"os"
	"strings"
	"time"
)

func (s *Server) handle(req Request) {
    switch req.Method {
	case "initialize":
		s.handleInitialize(req)
	case "initialized":
		s.handleInitialized()
	case "shutdown":
		s.handleShutdown(req)
	case "exit":
		s.handleExit()
	case "textDocument/didOpen":
		s.handleDidOpen(req)
	case "textDocument/didChange":
		s.handleDidChange(req)
	case "textDocument/didClose":
		s.handleDidClose(req)
	case "textDocument/completion":
		s.handleCompletion(req)
    case "textDocument/codeAction":
        s.handleCodeAction(req)
    case "codeAction/resolve":
        s.handleCodeActionResolve(req)
    default:
		if len(req.ID) != 0 {
			s.reply(req.ID, nil, &RespError{Code: -32601, Message: fmt.Sprintf("method not found: %s", req.Method)})
		}
	}
}

func (s *Server) handleInitialize(req Request) {
	version := internal.Version
	if s.llmClient != nil {
		version = version + " [" + s.llmClient.Name() + ":" + s.llmClient.DefaultModel() + "]"
	}
    res := InitializeResult{
        Capabilities: ServerCapabilities{
            TextDocumentSync: 1, // 1 = TextDocumentSyncKindFull
            CompletionProvider: &CompletionOptions{
                ResolveProvider:   false,
                TriggerCharacters: s.triggerChars,
            },
            CodeActionProvider: CodeActionOptions{ResolveProvider: true},
        },
        ServerInfo: &ServerInfo{Name: "hexai", Version: version},
    }
	s.reply(req.ID, res, nil)
}

func (s *Server) handleCodeAction(req Request) {
	var p CodeActionParams
	if err := json.Unmarshal(req.Params, &p); err != nil {
		if len(req.ID) != 0 {
			s.reply(req.ID, []CodeAction{}, nil)
		}
		return
	}
	d := s.getDocument(p.TextDocument.URI)
	if d == nil || len(d.lines) == 0 || s.llmClient == nil {
		if len(req.ID) != 0 {
			s.reply(req.ID, []CodeAction{}, nil)
		}
		return
	}
	sel := extractRangeText(d, p.Range)
	if strings.TrimSpace(sel) == "" {
		if len(req.ID) != 0 {
			s.reply(req.ID, []CodeAction{}, nil)
		}
		return
	}

	actions := make([]CodeAction, 0, 2)
	if a := s.buildRewriteCodeAction(p, sel); a != nil {
		actions = append(actions, *a)
	}
	if a := s.buildDiagnosticsCodeAction(p, sel); a != nil {
		actions = append(actions, *a)
	}
	if len(req.ID) != 0 {
		s.reply(req.ID, actions, nil)
	}
}

func (s *Server) buildRewriteCodeAction(p CodeActionParams, sel string) *CodeAction {
    if instr, cleaned := instructionFromSelection(sel); strings.TrimSpace(instr) != "" {
        payload := struct{
            Type string `json:"type"`
            URI  string `json:"uri"`
            Range Range `json:"range"`
            Instruction string `json:"instruction"`
            Selection string `json:"selection"`
        }{Type: "rewrite", URI: p.TextDocument.URI, Range: p.Range, Instruction: instr, Selection: cleaned}
        raw, _ := json.Marshal(payload)
        ca := CodeAction{Title: "Hexai: rewrite selection", Kind: "refactor.rewrite", Data: raw}
        return &ca
    }
    return nil
}

func (s *Server) buildDiagnosticsCodeAction(p CodeActionParams, sel string) *CodeAction {
    diags := s.diagnosticsInRange(p.Context, p.Range)
    if len(diags) == 0 {
        return nil
    }
    payload := struct{
        Type string `json:"type"`
        URI  string `json:"uri"`
        Range Range `json:"range"`
        Selection string `json:"selection"`
        Diagnostics []Diagnostic `json:"diagnostics"`
    }{Type: "diagnostics", URI: p.TextDocument.URI, Range: p.Range, Selection: sel, Diagnostics: diags}
    raw, _ := json.Marshal(payload)
    ca := CodeAction{Title: "Hexai: resolve diagnostics", Kind: "quickfix", Data: raw}
    return &ca
}

func (s *Server) resolveCodeAction(ca CodeAction) (CodeAction, bool) {
    if s.llmClient == nil || len(ca.Data) == 0 {
        return ca, false
    }
    var payload struct{
        Type string `json:"type"`
        URI  string `json:"uri"`
        Range Range `json:"range"`
        Instruction string `json:"instruction,omitempty"`
        Selection string `json:"selection"`
        Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
    }
    if err := json.Unmarshal(ca.Data, &payload); err != nil {
        return ca, false
    }
    switch payload.Type {
    case "rewrite":
        sys := "You are a precise code refactoring engine. Rewrite the given code strictly according to the instruction. Return only the updated code with no prose or backticks. Preserve formatting where reasonable."
        user := fmt.Sprintf("Instruction: %s\n\nSelected code to transform:\n%s", payload.Instruction, payload.Selection)
        ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
        messages := []llm.Message{{Role: "system", Content: sys}, {Role: "user", Content: user}}
        opts := s.llmRequestOpts()
        if text, err := s.llmClient.Chat(ctx, messages, opts...); err == nil {
            if out := stripCodeFences(strings.TrimSpace(text)); out != "" {
                edit := WorkspaceEdit{Changes: map[string][]TextEdit{payload.URI: {{Range: payload.Range, NewText: out}}}}
                ca.Edit = &edit
                return ca, true
            }
        } else {
            logging.Logf("lsp ", "codeAction rewrite llm error: %v", err)
        }
    case "diagnostics":
        sys := "You are a precise code fixer. Resolve the given diagnostics by editing only the selected code. Return only the corrected code with no prose or backticks. Keep behavior and style, and avoid unrelated changes."
        var b strings.Builder
        b.WriteString("Diagnostics to resolve (selection only):\n")
        for i, dgn := range payload.Diagnostics {
            if dgn.Source != "" {
                fmt.Fprintf(&b, "%d. [%s] %s\n", i+1, dgn.Source, dgn.Message)
            } else {
                fmt.Fprintf(&b, "%d. %s\n", i+1, dgn.Message)
            }
        }
        b.WriteString("\nSelected code:\n")
        b.WriteString(payload.Selection)
        ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second)
        defer cancel()
        messages := []llm.Message{{Role: "system", Content: sys}, {Role: "user", Content: b.String()}}
        opts := s.llmRequestOpts()
        if text, err := s.llmClient.Chat(ctx, messages, opts...); err == nil {
            if out := stripCodeFences(strings.TrimSpace(text)); out != "" {
                edit := WorkspaceEdit{Changes: map[string][]TextEdit{payload.URI: {{Range: payload.Range, NewText: out}}}}
                ca.Edit = &edit
                return ca, true
            }
        } else {
            logging.Logf("lsp ", "codeAction diagnostics llm error: %v", err)
        }
    }
    return ca, false
}

func (s *Server) handleCodeActionResolve(req Request) {
    var ca CodeAction
    if err := json.Unmarshal(req.Params, &ca); err != nil {
        if len(req.ID) != 0 {
            s.reply(req.ID, ca, nil)
        }
        return
    }
    if resolved, ok := s.resolveCodeAction(ca); ok {
        s.reply(req.ID, resolved, nil)
        return
    }
    s.reply(req.ID, ca, nil)
}

func (s *Server) llmRequestOpts() []llm.RequestOption {
	opts := []llm.RequestOption{llm.WithMaxTokens(s.maxTokens)}
	if s.codingTemperature != nil {
		opts = append(opts, llm.WithTemperature(*s.codingTemperature))
	}
	return opts
}

// instructionFromSelection extracts the first instruction from selection text.
// Preference order on each line: strict ;text; marker (no inner spaces), then
// a line comment (//, #, --). Returns the instruction string and the selection
// text cleaned of the matched instruction marker or comment.
func instructionFromSelection(sel string) (string, string) {
	lines := splitLines(sel)
	for idx, line := range lines {
		if instr, cleaned, ok := findFirstInstructionInLine(line); ok && strings.TrimSpace(instr) != "" {
			lines[idx] = cleaned
			return instr, strings.Join(lines, "\n")
		}
	}
	return "", sel
}

// findFirstInstructionInLine returns the earliest instruction marker on the
// line and the line with that marker removed. Supported markers, ordered by
// earliest byte offset in the line:
// - ;text; (strict, no space after first ';' or before last ';')
// - /* text */ (single-line only)
// - <!-- text --> (single-line only)
// - // text
// - # text
// - -- text
func findFirstInstructionInLine(line string) (instr string, cleaned string, ok bool) {
	type cand struct {
		start, end int
		text       string
	}
	cands := []cand{}
	if t, l, r, ok := findStrictSemicolonTag(line); ok {
		cands = append(cands, cand{start: l, end: r, text: t})
	}
	if i := strings.Index(line, "/*"); i >= 0 {
		if j := strings.Index(line[i+2:], "*/"); j >= 0 {
			start := i
			end := i + 2 + j + 2
			text := strings.TrimSpace(line[i+2 : i+2+j])
			cands = append(cands, cand{start: start, end: end, text: text})
		}
	}
	if i := strings.Index(line, "<!--"); i >= 0 {
		if j := strings.Index(line[i+4:], "-->"); j >= 0 {
			start := i
			end := i + 4 + j + 3
			text := strings.TrimSpace(line[i+4 : i+4+j])
			cands = append(cands, cand{start: start, end: end, text: text})
		}
	}
	if i := strings.Index(line, "//"); i >= 0 {
		cands = append(cands, cand{start: i, end: len(line), text: strings.TrimSpace(line[i+2:])})
	}
	if i := strings.Index(line, "#"); i >= 0 {
		cands = append(cands, cand{start: i, end: len(line), text: strings.TrimSpace(line[i+1:])})
	}
	if i := strings.Index(line, "--"); i >= 0 {
		cands = append(cands, cand{start: i, end: len(line), text: strings.TrimSpace(line[i+2:])})
	}
	if len(cands) == 0 {
		return "", line, false
	}
	// pick earliest start index
	best := cands[0]
	for _, c := range cands[1:] {
		if c.start >= 0 && (best.start < 0 || c.start < best.start) {
			best = c
		}
	}
	cleaned = strings.TrimRight(line[:best.start]+line[best.end:], " \t")
	return best.text, cleaned, true
}

// findStrictSemicolonTag finds ;text; with no space after first ';' and no space
// before the last ';' on the given line. Returns the text between semicolons,
// the start index of the opening ';', the end index just after the closing ';',
// and whether it was found.
func findStrictSemicolonTag(line string) (string, int, int, bool) {
	pos := 0
	for pos < len(line) {
		j := strings.Index(line[pos:], ";")
		if j < 0 {
			return "", 0, 0, false
		}
		j += pos
		// ensure single ';' (not ';;') and non-space after
		if j+1 >= len(line) || line[j+1] == ';' || line[j+1] == ' ' {
			pos = j + 1
			continue
		}
		k := strings.Index(line[j+1:], ";")
		if k < 0 {
			return "", 0, 0, false
		}
		closeIdx := j + 1 + k
		if closeIdx-1 < 0 || line[closeIdx-1] == ' ' {
			pos = closeIdx + 1
			continue
		}
		inner := strings.TrimSpace(line[j+1 : closeIdx])
		if inner == "" {
			pos = closeIdx + 1
			continue
		}
		end := closeIdx + 1
		return inner, j, end, true
	}
	return "", 0, 0, false
}

// diagnosticsInRange parses the CodeAction context and returns diagnostics
// that overlap the given selection range. If the context is missing or does
// not contain diagnostics, returns an empty slice.
func (s *Server) diagnosticsInRange(ctxRaw json.RawMessage, sel Range) []Diagnostic {
	if len(ctxRaw) == 0 {
		return nil
	}
	var ctx CodeActionContext
	if err := json.Unmarshal(ctxRaw, &ctx); err != nil {
		return nil
	}
	if len(ctx.Diagnostics) == 0 {
		return nil
	}
	out := make([]Diagnostic, 0, len(ctx.Diagnostics))
	for _, d := range ctx.Diagnostics {
		if rangesOverlap(d.Range, sel) {
			out = append(out, d)
		}
	}
	return out
}

// rangesOverlap reports whether two LSP ranges overlap at all.
func rangesOverlap(a, b Range) bool {
	// Normalize ordering
	if greaterPos(a.Start, a.End) {
		a.Start, a.End = a.End, a.Start
	}
	if greaterPos(b.Start, b.End) {
		b.Start, b.End = b.End, b.Start
	}
	// a ends before b starts
	if lessPos(a.End, b.Start) {
		return false
	}
	// b ends before a starts
	if lessPos(b.End, a.Start) {
		return false
	}
	return true
}

func lessPos(p, q Position) bool {
	if p.Line != q.Line {
		return p.Line < q.Line
	}
	return p.Character < q.Character
}

func greaterPos(p, q Position) bool {
	if p.Line != q.Line {
		return p.Line > q.Line
	}
	return p.Character > q.Character
}

// extractRangeText returns the exact text within the given document range.
func extractRangeText(d *document, r Range) string {
	if r.Start.Line == r.End.Line {
		line := d.lines[r.Start.Line]
		if r.Start.Character < 0 {
			r.Start.Character = 0
		}
		if r.End.Character > len(line) {
			r.End.Character = len(line)
		}
		if r.Start.Character > r.End.Character {
			return ""
		}
		return line[r.Start.Character:r.End.Character]
	}
	var b strings.Builder
	// first line
	first := d.lines[r.Start.Line]
	if r.Start.Character < 0 {
		r.Start.Character = 0
	}
	if r.Start.Character > len(first) {
		r.Start.Character = len(first)
	}
	b.WriteString(first[r.Start.Character:])
	b.WriteString("\n")
	// middle lines
	for i := r.Start.Line + 1; i < r.End.Line; i++ {
		b.WriteString(d.lines[i])
		if i+1 <= r.End.Line {
			b.WriteString("\n")
		}
	}
	// last line
	last := d.lines[r.End.Line]
	if r.End.Character < 0 {
		r.End.Character = 0
	}
	if r.End.Character > len(last) {
		r.End.Character = len(last)
	}
	b.WriteString(last[:r.End.Character])
	return b.String()
}

func (s *Server) handleInitialized() {
	logging.Logf("lsp ", "client initialized")
}

func (s *Server) handleShutdown(req Request) {
	s.reply(req.ID, nil, nil)
}

func (s *Server) handleExit() {
	s.exited = true
	os.Exit(0)
}

func (s *Server) handleDidOpen(req Request) {
	var p DidOpenTextDocumentParams
	if err := json.Unmarshal(req.Params, &p); err == nil {
		s.setDocument(p.TextDocument.URI, p.TextDocument.Text)
		s.markActivity()
	}
}

func (s *Server) handleDidChange(req Request) {
    var p DidChangeTextDocumentParams
    if err := json.Unmarshal(req.Params, &p); err == nil {
        if len(p.ContentChanges) > 0 {
            s.setDocument(p.TextDocument.URI, p.ContentChanges[len(p.ContentChanges)-1].Text)
        }
        s.markActivity()
        // Detect in-editor chat trigger lines and respond inline.
        s.detectAndHandleChat(p.TextDocument.URI)
    }
}

func (s *Server) handleDidClose(req Request) {
	var p DidCloseTextDocumentParams
	if err := json.Unmarshal(req.Params, &p); err == nil {
		s.deleteDocument(p.TextDocument.URI)
		s.markActivity()
	}
}

func (s *Server) handleCompletion(req Request) {
	var p CompletionParams
	var docStr string
	if err := json.Unmarshal(req.Params, &p); err == nil {
		above, current, below, funcCtx := s.lineContext(p.TextDocument.URI, p.Position)
		docStr = s.buildDocString(p, above, current, below, funcCtx)
		if s.logContext {
			s.logCompletionContext(p, above, current, below, funcCtx)
		}
		if s.llmClient != nil {
			newFunc := s.isDefiningNewFunction(p.TextDocument.URI, p.Position)
			extra, has := s.buildAdditionalContext(newFunc, p.TextDocument.URI, p.Position)
            items, ok, busy := s.tryLLMCompletion(p, above, current, below, funcCtx, docStr, has, extra)
            if ok {
                s.reply(req.ID, CompletionList{IsIncomplete: false, Items: items}, nil)
                return
            }
            if busy {
                // Inform client that results are incomplete so it may try again shortly.
                logging.Logf("lsp ", "completion busy uri=%s line=%d char=%d returning isIncomplete", p.TextDocument.URI, p.Position.Line, p.Position.Character)
                s.reply(req.ID, CompletionList{IsIncomplete: true, Items: []CompletionItem{}}, nil)
                return
            }
		}
	}
	items := s.fallbackCompletionItems(docStr)
	s.reply(req.ID, CompletionList{IsIncomplete: false, Items: items}, nil)
}

func (s *Server) reply(id json.RawMessage, result any, err *RespError) {
    resp := Response{JSONRPC: "2.0", ID: id, Result: result, Error: err}
    s.writeMessage(resp)
}

// --- in-editor chat (";C ...") ---

// detectAndHandleChat scans the current document for any line that starts with
// ";C" and appears to be awaiting a response (i.e., followed by a blank line
// and no non-empty answer line yet). If found, it asks the LLM and inserts the
// answer below the blank line, leaving exactly one empty line between prompt
// and response.
func (s *Server) detectAndHandleChat(uri string) {
    if s.llmClient == nil {
        return
    }
    d := s.getDocument(uri)
    if d == nil || len(d.lines) == 0 {
        return
    }
    for i, raw := range d.lines {
        // Find last non-space character index
        j := len(raw) - 1
        for j >= 0 {
            if raw[j] == ' ' || raw[j] == '\t' { j--; continue }
            break
        }
        if j < 1 { // need at least two chars for double trigger
            continue
        }
        pair := raw[j-1 : j+1]
        isTrigger := pair == ".." || pair == "??" || pair == "!!" || pair == "::"
        if !isTrigger {
            continue
        }
        // Avoid double-answering: if the next non-empty line starts with '>' we skip.
        k := i + 1
        for k < len(d.lines) && strings.TrimSpace(d.lines[k]) == "" { k++ }
        if k < len(d.lines) && strings.HasPrefix(strings.TrimSpace(d.lines[k]), ">") {
            continue
        }
        // Derive prompt by removing 1 trailing char for punctuation pairs
        removeCount := 1
        base := raw[:j+1-removeCount]
        prompt := strings.TrimSpace(base)
        if prompt == "" {
            continue
        }
        if !s.tryStartLLM() {
            continue
        }
        lineIdx := i
        lastIdx := j
        go func(prompt string, remove int) {
            defer s.endLLM()
            ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
            defer cancel()
            sys := "You are a helpful coding assistant. Answer concisely and clearly."
            // Build short conversation history from the document above this line
            history := s.buildChatHistory(uri, lineIdx, prompt)
            msgs := append([]llm.Message{{Role: "system", Content: sys}}, history...)
            opts := s.llmRequestOpts()
            logging.Logf("lsp ", "chat llm=requesting model=%s", s.llmClient.DefaultModel())
            text, err := s.llmClient.Chat(ctx, msgs, opts...)
            if err != nil {
                logging.Logf("lsp ", "chat llm error: %v", err)
                return
            }
            out := strings.TrimSpace(stripCodeFences(text))
            if out == "" {
                return
            }
            s.applyChatEdits(uri, lineIdx, lastIdx, remove, "> "+out)
        }(prompt, removeCount)
        // Only handle one per change tick to avoid flooding
        break
    }
}

// applyChatEdits removes the triggering punctuation at end of the line and
// inserts two newlines followed by a new line with the response prefixed.
func (s *Server) applyChatEdits(uri string, lineIdx int, lastNonSpace int, removeCount int, response string) {
    d := s.getDocument(uri)
    if d == nil { return }
    // 1) Delete the trailing punctuation (1 or 2 chars)
    delStart := Position{Line: lineIdx, Character: lastNonSpace+1-removeCount}
    delEnd := Position{Line: lineIdx, Character: lastNonSpace+1}
    // 2) Insert two newlines and the response at end-of-line, then one extra
    //    newline so there is exactly one blank line after the reply
    insPos := Position{Line: lineIdx, Character: len(d.lines[lineIdx])}
    resp := strings.TrimRight(response, "\n") + "\n"
    insert := "\n\n" + resp + "\n"
    edits := []TextEdit{
        {Range: Range{Start: delStart, End: delEnd}, NewText: ""},
        {Range: Range{Start: insPos, End: insPos}, NewText: insert},
    }
    we := WorkspaceEdit{Changes: map[string][]TextEdit{uri: edits}}
    s.clientApplyEdit("Hexai: insert chat response", we)
}

// buildChatHistory walks upwards from the current line to collect the most recent
// Q/A pairs in the in-editor transcript. It returns messages in chronological order
// ending with the current user prompt. Limits to a small number of pairs to control tokens.
func (s *Server) buildChatHistory(uri string, lineIdx int, currentPrompt string) []llm.Message {
    d := s.getDocument(uri)
    if d == nil { return []llm.Message{{Role: "user", Content: currentPrompt}} }
    type pair struct{ q string; a string }
    pairs := []pair{}
    i := lineIdx - 1
    // Collect up to 3 recent pairs
    for i >= 0 && len(pairs) < 3 {
        // Skip blank lines
        for i >= 0 && strings.TrimSpace(d.lines[i]) == "" { i-- }
        if i < 0 { break }
        // Expect assistant reply lines starting with ">"
        if !strings.HasPrefix(strings.TrimSpace(d.lines[i]), ">") {
            break
        }
        // Collect contiguous reply block
        var replyLines []string
        for i >= 0 {
            line := strings.TrimSpace(d.lines[i])
            if strings.HasPrefix(line, ">") {
                replyLines = append([]string{strings.TrimSpace(strings.TrimPrefix(line, ">"))}, replyLines...)
                i--
                continue
            }
            break
        }
        // Skip a single blank line that should separate Q from A
        for i >= 0 && strings.TrimSpace(d.lines[i]) == "" { i-- }
        if i < 0 { break }
        // Take the question as the non-empty line above
        q := strings.TrimSpace(d.lines[i])
        // Remove any lingering trigger pair at end if present
        q = stripTrailingTrigger(q)
        pairs = append([]pair{{q: q, a: strings.Join(replyLines, "\n")}}, pairs...)
        i--
        // Continue to find older pairs
    }
    // Build messages
    msgs := make([]llm.Message, 0, len(pairs)*2+1)
    for _, p := range pairs {
        if strings.TrimSpace(p.q) != "" {
            msgs = append(msgs, llm.Message{Role: "user", Content: p.q})
        }
        if strings.TrimSpace(p.a) != "" {
            msgs = append(msgs, llm.Message{Role: "assistant", Content: p.a})
        }
    }
    msgs = append(msgs, llm.Message{Role: "user", Content: currentPrompt})
    return msgs
}

// stripTrailingTrigger removes a single trailing punctuation from the set
// [.,?,!,:] or both semicolons if present at end, mirroring the inline trigger rules.
func stripTrailingTrigger(sx string) string {
    s := strings.TrimRight(sx, " \t")
    if strings.HasSuffix(s, ";;") {
        return strings.TrimRight(strings.TrimSuffix(s, ";;"), " \t")
    }
    if len(s) == 0 { return sx }
    last := s[len(s)-1]
    switch last {
    case '.', '?', '!', ':':
        return strings.TrimRight(s[:len(s)-1], " \t")
    default:
        return sx
    }
}

// clientApplyEdit sends a workspace/applyEdit request to the client.
func (s *Server) clientApplyEdit(label string, edit WorkspaceEdit) {
    params := ApplyWorkspaceEditParams{Label: label, Edit: edit}
    // Build a JSON-RPC request with a fresh id
    id := s.nextReqID()
    req := Request{JSONRPC: "2.0", ID: id, Method: "workspace/applyEdit"}
    // marshal params separately to avoid changing Request type
    b, _ := json.Marshal(params)
    req.Params = b
    s.writeMessage(req)
}

// nextReqID returns a unique json.RawMessage id for server-initiated requests.
func (s *Server) nextReqID() json.RawMessage {
    s.mu.Lock()
    s.nextID++
    idNum := s.nextID
    s.mu.Unlock()
    b, _ := json.Marshal(idNum)
    return b
}

// --- completion helpers ---

func (s *Server) buildDocString(p CompletionParams, above, current, below, funcCtx string) string {
	return fmt.Sprintf("file: %s\nline: %d\nabove: %s\ncurrent: %s\nbelow: %s\nfunction: %s",
		p.TextDocument.URI, p.Position.Line, trimLen(above), trimLen(current), trimLen(below), trimLen(funcCtx))
}

func (s *Server) logCompletionContext(p CompletionParams, above, current, below, funcCtx string) {
	logging.Logf("lsp ", "completion ctx uri=%s line=%d char=%d above=%q current=%q below=%q function=%q",
		p.TextDocument.URI, p.Position.Line, p.Position.Character, trimLen(above), trimLen(current), trimLen(below), trimLen(funcCtx))
}

func (s *Server) tryLLMCompletion(p CompletionParams, above, current, below, funcCtx, docStr string, hasExtra bool, extraText string) ([]CompletionItem, bool, bool) {
    ctx, cancel := context.WithTimeout(context.Background(), 6*time.Second)
    defer cancel()

    // Inline prompt markers (strict ;text; or double-; patterns) explicitly allow triggering.
    inlinePrompt := lineHasInlinePrompt(current)
    // Only invoke LLM when triggered by our characters, manual invoke, or inline prompt markers.
    if !inlinePrompt && !s.isTriggerEvent(p, current) {
        logging.Logf("lsp ", "%scompletion skip=no-trigger line=%d char=%d current=%q%s", logging.AnsiYellow, p.Position.Line, p.Position.Character, trimLen(current), logging.AnsiBase)
        return []CompletionItem{}, true, false
    }

    inParams := inParamList(current, p.Position.Character)

    // Detect manual invoke so we can relax prefix heuristics when user pressed completion key.
    manualInvoke := false
    if p.Context != nil {
        var c struct{
            TriggerKind int `json:"triggerKind"`
        }
        if raw, ok := p.Context.(json.RawMessage); ok {
            _ = json.Unmarshal(raw, &c)
        } else {
            b, _ := json.Marshal(p.Context)
            _ = json.Unmarshal(b, &c)
        }
        if c.TriggerKind == 1 { // Invoked
            manualInvoke = true
        }
    }

    // Build a cache key for this completion context (ignore trailing whitespace
    // before the cursor when forming the key) and try cache before any LLM call.
    key := s.completionCacheKey(p, above, current, below, funcCtx, inParams, hasExtra, extraText)
    if cleaned, ok := s.completionCacheGet(key); ok && strings.TrimSpace(cleaned) != "" {
        logging.Logf("lsp ", "completion cache hit uri=%s line=%d char=%d preview=%s%s%s",
            p.TextDocument.URI, p.Position.Line, p.Position.Character,
            logging.AnsiGreen, logging.PreviewForLog(cleaned), logging.AnsiBase)
        return s.makeCompletionItems(cleaned, inParams, current, p, docStr), true, false
    }
    // If there is a bare ';;' on the current or next line (no valid ';;text;'),
    // do not auto-trigger unless it was a manual invoke.
    if (isBareDoubleSemicolon(current) || isBareDoubleSemicolon(below)) && !manualInvoke {
        logging.Logf("lsp ", "%scompletion skip=empty-double-semicolon line=%d char=%d current=%q%s", logging.AnsiYellow, p.Position.Line, p.Position.Character, trimLen(current), logging.AnsiBase)
        return []CompletionItem{}, true, false
    }

    // Heuristic 1: Require a minimal typed identifier prefix to avoid early triggers,
    // but allow immediate completion after structural trigger chars like '.', ':', '/'.
    if !inParams {
        // Determine the effective cursor index within current line, clamped, and
        // skip over trailing spaces/tabs to support cases like "type Matrix| "
        // where the cursor is after a space following an identifier.
        idx := p.Position.Character
        if idx > len(current) { idx = len(current) }
        // Structural triggers allow no prefix
        allowNoPrefix := false
        if manualInvoke || inlinePrompt {
            allowNoPrefix = true
        }
        if idx > 0 {
            ch := current[idx-1]
            if ch == '.' || ch == ':' || ch == '/' || ch == '_' || ch == ')' {
                allowNoPrefix = true
            }
        }
        if !allowNoPrefix {
            // Walk left over whitespace
            j := idx
            for j > 0 {
                c := current[j-1]
                if c == ' ' || c == '\t' { j--; continue }
                break
            }
            start := computeWordStart(current, j)
            if j-start < 1 { // require at least 1 identifier char
                logging.Logf("lsp ", "%scompletion skip=short-prefix line=%d char=%d current=%q%s", logging.AnsiYellow, p.Position.Line, p.Position.Character, trimLen(current), logging.AnsiBase)
                return []CompletionItem{}, true, false
            }
        }
    }
    // Concurrency guard: if another LLM request is running, skip this one.
    if !s.tryStartLLM() {
        logging.Logf("lsp ", "%scompletion skip=busy another LLM request in flight%s", logging.AnsiYellow, logging.AnsiBase)
        return []CompletionItem{}, false, true
    }
    sysPrompt, userPrompt := buildPrompts(inParams, p, above, current, below, funcCtx)
    messages := []llm.Message{
        {Role: "system", Content: sysPrompt},
        {Role: "user", Content: userPrompt},
    }
	if hasExtra && extraText != "" {
		messages = append(messages, llm.Message{Role: "user", Content: "Additional context:\n" + extraText})
	}

    // If an inline prompt marker is present, make the instruction stricter: code only.
    if inlinePrompt {
        messages[0].Content = "You are a precise code completion/refactoring engine. Output only the code to insert with no prose, no comments, and no backticks. Return raw code only."
    }

    // Compute total sent context size (sum of message contents)
	var sentSize int
	for _, m := range messages {
		sentSize += len(m.Content)
	}
	s.incSentCounters(sentSize)

	opts := []llm.RequestOption{llm.WithMaxTokens(s.maxTokens)}
	if s.codingTemperature != nil {
		opts = append(opts, llm.WithTemperature(*s.codingTemperature))
	}
    logging.Logf("lsp ", "completion llm=requesting model=%s", s.llmClient.DefaultModel())
    text, err := s.llmClient.Chat(ctx, messages, opts...)
    defer s.endLLM()
	if err != nil {
		logging.Logf("lsp ", "llm completion error: %v", err)
		// Log updated averages after this request (even if failed)
		s.logLLMStats()
        return nil, false, false
	}
	// Update response counters (received)
	s.incRecvCounters(len(text))
	s.logLLMStats()
    cleaned := stripCodeFences(strings.TrimSpace(text))
    // For code completion responses, also strip inline single-backtick code spans
    // when the model returns prose like: "Use `expr` here".
    if cleaned != "" {
        if strings.ContainsRune(cleaned, '`') {
            inline := stripInlineCodeSpan(cleaned)
            if strings.TrimSpace(inline) != "" {
                cleaned = inline
            }
        }
    }
    if cleaned != "" { cleaned = stripDuplicateAssignmentPrefix(current[:p.Position.Character], cleaned) }
    if cleaned != "" { cleaned = stripDuplicateGeneralPrefix(current[:p.Position.Character], cleaned) }
    // Preserve the current line's leading indentation only for double-semicolon
    // inline prompts (";;text;"), since strict ";text;" replacements already
    // occur in-place without affecting leading indentation.
    if cleaned != "" && hasDoubleSemicolonTrigger(current) {
        indent := leadingIndent(current)
        if indent != "" {
            cleaned = applyIndent(indent, cleaned)
        }
    }
    if cleaned == "" {
        return nil, false, false
    }

    // Store successful completion in cache
    s.completionCachePut(key, cleaned)

    return s.makeCompletionItems(cleaned, inParams, current, p, docStr), true, false
}

// --- small completion cache (last ~10 entries) ---

func (s *Server) completionCacheKey(p CompletionParams, above, current, below, funcCtx string, inParams bool, hasExtra bool, extraText string) string {
    // Normalize left-of-cursor by trimming trailing spaces/tabs
    idx := p.Position.Character
    if idx > len(current) { idx = len(current) }
    left := strings.TrimRight(current[:idx], " \t")
    right := ""
    if idx < len(current) { right = current[idx:] }
    prov := ""
    model := ""
    if s.llmClient != nil {
        prov = s.llmClient.Name()
        model = s.llmClient.DefaultModel()
    }
    temp := ""
    if s.codingTemperature != nil { temp = fmt.Sprintf("%.3f", *s.codingTemperature) }
    extra := ""
    if hasExtra {
        extra = strings.TrimSpace(extraText)
    }
    // Compose a key from essential context parts
    return strings.Join([]string{
        "v1", // version for future-proofing
        prov,
        model,
        temp,
        p.TextDocument.URI,
        fmt.Sprintf("%d:%d", p.Position.Line, len(left)),
        above,
        left,
        right,
        below,
        funcCtx,
        fmt.Sprintf("params=%t", inParams),
        extra,
    }, "\x1f") // use unit separator to avoid collisions
}

func (s *Server) completionCacheGet(key string) (string, bool) {
    s.mu.Lock()
    defer s.mu.Unlock()
    v, ok := s.compCache[key]
    if !ok {
        return "", false
    }
    // move to most-recent
    s.compCacheTouchLocked(key)
    return v, true
}

func (s *Server) completionCachePut(key, value string) {
    s.mu.Lock()
    defer s.mu.Unlock()
    if s.compCache == nil {
        s.compCache = make(map[string]string)
    }
    if _, exists := s.compCache[key]; !exists {
        s.compCacheOrder = append(s.compCacheOrder, key)
        s.compCache[key] = value
        if len(s.compCacheOrder) > 10 {
            // evict oldest
            old := s.compCacheOrder[0]
            s.compCacheOrder = s.compCacheOrder[1:]
            delete(s.compCache, old)
        }
        return
    }
    // update existing and mark most-recent
    s.compCache[key] = value
    s.compCacheTouchLocked(key)
}

func (s *Server) compCacheTouchLocked(key string) {
    // assumes s.mu is held
    // remove any existing occurrence of key in order slice
    idx := -1
    for i, k := range s.compCacheOrder {
        if k == key { idx = i; break }
    }
    if idx >= 0 {
        s.compCacheOrder = append(append([]string{}, s.compCacheOrder[:idx]...), s.compCacheOrder[idx+1:]...)
    }
    s.compCacheOrder = append(s.compCacheOrder, key)
}

// isTriggerEvent returns true when the completion request appears to be caused
// by typing one of our configured trigger characters. It checks the LSP
// CompletionContext if provided and also falls back to inspecting the character
// immediately to the left of the cursor.
func (s *Server) isTriggerEvent(p CompletionParams, current string) bool {
    // 1) Inspect LSP completion context if present
    if p.Context != nil {
        var ctx struct{
            TriggerKind int    `json:"triggerKind"`
            TriggerCharacter string `json:"triggerCharacter,omitempty"`
        }
        if raw, ok := p.Context.(json.RawMessage); ok {
            _ = json.Unmarshal(raw, &ctx)
        } else {
            b, _ := json.Marshal(p.Context)
            _ = json.Unmarshal(b, &ctx)
        }
        // If the line contains a bare ';;' (no ';;text;'), do not treat as a trigger source.
        if strings.Contains(current, ";;") && !hasDoubleSemicolonTrigger(current) {
            return false
        }
        // TriggerKind 1 = Invoked (manual) — always allow (unless bare ';;' above)
        if ctx.TriggerKind == 1 {
            return true
        }
        // TriggerKind 2 is TriggerCharacter per LSP spec
        if ctx.TriggerKind == 2 {
            if ctx.TriggerCharacter != "" {
                for _, c := range s.triggerChars {
                    if c == ctx.TriggerCharacter {
                        return true
                    }
                }
                return false
            }
            // No character provided but reported as TriggerCharacter; be conservative
            return false
        }
        // For TriggerForIncomplete (3), require manual char check below
    }
    // 2) Fallback: check the character immediately prior to cursor
    idx := p.Position.Character
    if idx <= 0 || idx > len(current) {
        return false
    }
    // Bare ';;' should not trigger via fallback char either
    if strings.Contains(current, ";;") && !hasDoubleSemicolonTrigger(current) {
        return false
    }
    ch := string(current[idx-1])
    for _, c := range s.triggerChars {
        if c == ch {
            return true
        }
    }
    return false
}

func (s *Server) makeCompletionItems(cleaned string, inParams bool, current string, p CompletionParams, docStr string) []CompletionItem {
	te, filter := computeTextEditAndFilter(cleaned, inParams, current, p)
	rm := s.collectPromptRemovalEdits(p.TextDocument.URI)
	label := labelForCompletion(cleaned, filter)
	detail := "Hexai LLM completion"
	if s.llmClient != nil {
		detail = "Hexai " + s.llmClient.Name() + ":" + s.llmClient.DefaultModel()
	}
	return []CompletionItem{{
		Label:               label,
		Kind:                1,
		Detail:              detail,
		InsertTextFormat:    1,
		FilterText:          strings.TrimLeft(filter, " \t"),
		TextEdit:            te,
		AdditionalTextEdits: rm,
		SortText:            "0000",
		Documentation:       docStr,
	}}
}

// small helpers to keep tryLLMCompletion short
func (s *Server) incSentCounters(n int) {
	s.mu.Lock()
	s.llmReqTotal++
	s.llmSentBytesTotal += int64(n)
	s.mu.Unlock()
}

func (s *Server) incRecvCounters(n int) {
	s.mu.Lock()
	s.llmRespTotal++
	s.llmRespBytesTotal += int64(n)
	s.mu.Unlock()
}

func (s *Server) logLLMStats() {
	s.mu.RLock()
	avgSent := int64(0)
	if s.llmReqTotal > 0 {
		avgSent = s.llmSentBytesTotal / s.llmReqTotal
	}
	avgRecv := int64(0)
	if s.llmRespTotal > 0 {
		avgRecv = s.llmRespBytesTotal / s.llmRespTotal
	}
	reqs, sentTot, recvTot := s.llmReqTotal, s.llmSentBytesTotal, s.llmRespBytesTotal
	s.mu.RUnlock()
	mins := time.Since(s.startTime).Minutes()
	if mins <= 0 {
		mins = 0.001
	}
	rpm := float64(reqs) / mins
	sentPerMin := float64(sentTot) / mins
	recvPerMin := float64(recvTot) / mins
	logging.Logf("lsp ", "llm stats reqs=%d avg_sent=%d avg_recv=%d sent_total=%d recv_total=%d rpm=%.2f sent_per_min=%.0f recv_per_min=%.0f", reqs, avgSent, avgRecv, sentTot, recvTot, rpm, sentPerMin, recvPerMin)
}

// collectPromptRemovalEdits returns edits to remove all inline prompt markers.
// Supported form (inclusive):
//   - ";...;" where there is no space immediately after the first ';'
//     and no space immediately before the last ';'. An optional single space
//     after the trailing ';' is also removed for cleanliness.
//
// Multiple markers per line are supported.
func (s *Server) collectPromptRemovalEdits(uri string) []TextEdit {
	d := s.getDocument(uri)
	if d == nil || len(d.lines) == 0 {
		return nil
	}
	var edits []TextEdit
	for i, line := range d.lines {
		edits = append(edits, promptRemovalEditsForLine(line, i)...)
	}
	return edits
}

func promptRemovalEditsForLine(line string, lineNum int) []TextEdit {
	if hasDoubleSemicolonTrigger(line) {
		return []TextEdit{{Range: Range{Start: Position{Line: lineNum, Character: 0}, End: Position{Line: lineNum, Character: len(line)}}, NewText: ""}}
	}
	return collectSemicolonMarkers(line, lineNum)
}

func hasDoubleSemicolonTrigger(line string) bool {
    pos := 0
    for pos < len(line) {
        j := strings.Index(line[pos:], ";;")
        if j < 0 {
            return false
        }
        j += pos
        contentStart := j + 2
        if contentStart >= len(line) {
            return false // nothing after ';;'
        }
        // First content char cannot be space or another ';'
        first := line[contentStart]
        if first == ' ' || first == ';' {
            pos = contentStart + 1
            continue
        }
        // Require at least one content char before a closing ';'
        k := strings.Index(line[contentStart+1:], ";")
        if k < 0 {
            return false
        }
        closeIdx := contentStart + 1 + k
        // Disallow trailing space before closing ';'
        if closeIdx-1 >= 0 && line[closeIdx-1] == ' ' {
            pos = closeIdx + 1
            continue
        }
        return true
    }
    return false
}

func collectSemicolonMarkers(line string, lineNum int) []TextEdit {
	var edits []TextEdit
	startSemi := 0
	for startSemi < len(line) {
		j := strings.Index(line[startSemi:], ";")
		if j < 0 {
			break
		}
		j += startSemi
		k := strings.Index(line[j+1:], ";")
		if k < 0 {
			break
		}
		if j+1 >= len(line) || line[j+1] == ' ' {
			startSemi = j + 1
			continue
		}
		if line[j+1] == ';' {
			startSemi = j + 2
			continue
		}
		closeIdx := j + 1 + k
		if closeIdx-1 < 0 || line[closeIdx-1] == ' ' {
			startSemi = closeIdx + 1
			continue
		}
		if closeIdx-(j+1) < 1 {
			startSemi = closeIdx + 1
			continue
		}
		endChar := closeIdx + 1
		if endChar < len(line) && line[endChar] == ' ' {
			endChar++
		}
		edits = append(edits, TextEdit{Range: Range{Start: Position{Line: lineNum, Character: j}, End: Position{Line: lineNum, Character: endChar}}, NewText: ""})
		startSemi = endChar
	}
	return edits
}

func inParamList(current string, cursor int) bool {
	if !strings.Contains(current, "func ") {
		return false
	}
	open := strings.Index(current, "(")
	close := strings.Index(current, ")")
	return open >= 0 && cursor > open && (close == -1 || cursor <= close)
}

func buildPrompts(inParams bool, p CompletionParams, above, current, below, funcCtx string) (string, string) {
	if inParams {
		sys := "You are a code completion engine for function signatures. Return only the parameter list contents (without parentheses), no braces, no prose. Prefer idiomatic names and types."
		user := fmt.Sprintf("Cursor is inside the function parameter list. Suggest only the parameter list (no parentheses).\nFunction line: %s\nCurrent line (cursor at %d): %s", funcCtx, p.Position.Character, current)
		return sys, user
	}
	sys := "You are a terse code completion engine. Return only the code to insert, no surrounding prose or backticks. Only continue from the cursor; never repeat characters already present to the left of the cursor on the current line (e.g., if 'name :=' is already typed, only return the right-hand side expression)."
	user := fmt.Sprintf("Provide the next likely code to insert at the cursor.\nFile: %s\nFunction/context: %s\nAbove line: %s\nCurrent line (cursor at character %d): %s\nBelow line: %s\nOnly return the completion snippet.", p.TextDocument.URI, funcCtx, above, p.Position.Character, current, below)
	return sys, user
}

func computeTextEditAndFilter(cleaned string, inParams bool, current string, p CompletionParams) (*TextEdit, string) {
	if inParams {
		open := strings.Index(current, "(")
		close := strings.Index(current, ")")
		if open >= 0 {
			left := open + 1
			right := len(current)
			if close >= 0 && close >= left {
				right = close
			}
			if p.Position.Character < right {
				right = p.Position.Character
			}
			te := &TextEdit{Range: Range{Start: Position{Line: p.Position.Line, Character: left}, End: Position{Line: p.Position.Line, Character: right}}, NewText: cleaned}
			var filter string
			if left >= 0 && right >= left && right <= len(current) {
				filter = strings.TrimLeft(current[left:right], " \t")
			}
			return te, filter
		}
	}
	startChar := computeWordStart(current, p.Position.Character)
	te := &TextEdit{Range: Range{Start: Position{Line: p.Position.Line, Character: startChar}, End: Position{Line: p.Position.Line, Character: p.Position.Character}}, NewText: cleaned}
	filter := strings.TrimLeft(current[startChar:p.Position.Character], " \t")
	return te, filter
}

func computeWordStart(current string, at int) int {
	if at > len(current) {
		at = len(current)
	}
	for at > 0 {
		ch := current[at-1]
		if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_' {
			at--
			continue
		}
		break
	}
	return at
}

func isIdentChar(ch byte) bool {
    return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_'
}

// lineHasInlinePrompt returns true if the line contains an inline strict
// semicolon marker ;text; (no spaces at boundaries) or a double-semicolon
// pattern recognized by hasDoubleSemicolonTrigger.
func lineHasInlinePrompt(line string) bool {
    if _, _, _, ok := findStrictSemicolonTag(line); ok {
        return true
    }
    return hasDoubleSemicolonTrigger(line)
}

// leadingIndent returns the run of leading spaces/tabs from the provided line.
func leadingIndent(line string) string {
    i := 0
    for i < len(line) {
        if line[i] == ' ' || line[i] == '\t' {
            i++
            continue
        }
        break
    }
    if i == 0 { return "" }
    return line[:i]
}

// applyIndent prefixes each non-empty line of suggestion with the given indent
// unless it already starts with that indent.
func applyIndent(indent, suggestion string) string {
    if indent == "" || suggestion == "" { return suggestion }
    lines := splitLines(suggestion)
    for i, ln := range lines {
        if strings.TrimSpace(ln) == "" { continue }
        if strings.HasPrefix(ln, indent) { continue }
        lines[i] = indent + ln
    }
    return strings.Join(lines, "\n")
}

// isBareDoubleSemicolon reports whether the line contains a standalone
// double-semicolon marker with no inline content (";;" possibly with only
// whitespace after it). It explicitly excludes the valid form ";;text;".
func isBareDoubleSemicolon(line string) bool {
    t := strings.TrimSpace(line)
    if !strings.Contains(t, ";;") {
        return false
    }
    if hasDoubleSemicolonTrigger(t) {
        return false
    }
    if strings.HasPrefix(t, ";;") {
        rest := strings.TrimSpace(t[2:])
        // Bare if nothing follows or only semicolons/spaces remain without closing pattern
        if rest == "" || rest == ";" {
            return true
        }
    }
    return false
}

// stripDuplicateAssignmentPrefix removes a duplicated assignment prefix (e.g.,
// "name :=") from the beginning of the model suggestion when that same prefix
// already appears immediately to the left of the cursor on the current line.
// Also handles simple '=' assignments.
func stripDuplicateAssignmentPrefix(prefixBeforeCursor, suggestion string) string {
	s2 := strings.TrimLeft(suggestion, " \t")
	// Prefer := if present at end of prefix
	if idx := strings.LastIndex(prefixBeforeCursor, ":="); idx >= 0 && idx+2 <= len(prefixBeforeCursor) {
		// Ensure only spaces follow in prefix (cursor at end of prefix segment)
		tail := prefixBeforeCursor[idx+2:]
		if strings.TrimSpace(tail) == "" {
			// Move left to include identifier and spaces
			start := idx - 1
			for start >= 0 && (isIdentChar(prefixBeforeCursor[start]) || prefixBeforeCursor[start] == ' ' || prefixBeforeCursor[start] == '\t') {
				start--
			}
			start++
			seg := strings.TrimRight(prefixBeforeCursor[start:idx+2], " \t")
			if strings.HasPrefix(s2, seg) {
				return strings.TrimLeft(s2[len(seg):], " \t")
			}
		}
	}
	// Fallback to plain '=' if present
	if idx := strings.LastIndex(prefixBeforeCursor, "="); idx >= 0 {
		if !(idx > 0 && prefixBeforeCursor[idx-1] == ':') { // not := (handled above)
			tail := prefixBeforeCursor[idx+1:]
			if strings.TrimSpace(tail) == "" {
				start := idx - 1
				for start >= 0 && (isIdentChar(prefixBeforeCursor[start]) || prefixBeforeCursor[start] == ' ' || prefixBeforeCursor[start] == '\t') {
					start--
				}
				start++
				seg := strings.TrimRight(prefixBeforeCursor[start:idx+1], " \t")
				if strings.HasPrefix(s2, seg) {
					return strings.TrimLeft(s2[len(seg):], " \t")
				}
			}
		}
	}
	return suggestion
}

// stripDuplicateGeneralPrefix removes any already-typed prefix that the model repeated
// at the beginning of its suggestion. It compares the entire text to the left of the
// cursor (prefixBeforeCursor) against the suggestion, trimming whitespace appropriately,
// and strips the longest sensible overlap. This prevents cases like:
//   prefix:    "func New "
//   suggestion:"func New() *Type"
// resulting in duplicates like "func New func New() *Type".
func stripDuplicateGeneralPrefix(prefixBeforeCursor, suggestion string) string {
    if suggestion == "" { return suggestion }
    s := strings.TrimLeft(suggestion, " \t")
    p := strings.TrimRight(prefixBeforeCursor, " \t")
    // Exact prefix overlap: remove the full typed prefix
    if p != "" && strings.HasPrefix(s, p) {
        return strings.TrimLeft(s[len(p):], " \t")
    }
    // Otherwise, try the longest token-aligned suffix of p that prefixes s
    // Prefer boundaries where the char before the suffix is not an identifier char
    for k := len(p) - 1; k > 0; k-- {
        if !isIdentBoundary(p[k-1]) { continue }
        suf := strings.TrimLeft(p[k:], " \t")
        if suf == "" { continue }
        if strings.HasPrefix(s, suf) {
            return strings.TrimLeft(s[len(suf):], " \t")
        }
    }
    return suggestion
}

func isIdentBoundary(ch byte) bool {
    return !((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_')
}

// stripCodeFences removes surrounding Markdown code fences from a model
// response when the entire output is wrapped, e.g. starting with "```go" or
// "```" and ending with "```". It returns the inner content unchanged.
func stripCodeFences(s string) string {
	t := strings.TrimSpace(s)
	if t == "" {
		return t
	}
	lines := splitLines(t)
	// find first and last non-empty lines
	start := 0
	for start < len(lines) && strings.TrimSpace(lines[start]) == "" {
		start++
	}
	end := len(lines) - 1
	for end >= 0 && strings.TrimSpace(lines[end]) == "" {
		end--
	}
	if start >= len(lines) || end < 0 || start > end {
		return t
	}
	first := strings.TrimSpace(lines[start])
	last := strings.TrimSpace(lines[end])
	if strings.HasPrefix(first, "```") && last == "```" && end > start {
		inner := strings.Join(lines[start+1:end], "\n")
		return inner
	}
	return t
}

// stripInlineCodeSpan returns only the contents of the first inline backtick
// code span if present, e.g., "some text `x := y()` more" -> "x := y()".
// If no matching pair of backticks exists, it returns the input unchanged.
// This is intended for code completion responses where the model may wrap a
// small snippet in single backticks among prose.
func stripInlineCodeSpan(s string) string {
    t := strings.TrimSpace(s)
    if t == "" {
        return t
    }
    i := strings.IndexByte(t, '`')
    if i < 0 {
        return t
    }
    jrel := strings.IndexByte(t[i+1:], '`')
    if jrel < 0 {
        return t
    }
    j := i + 1 + jrel
    return t[i+1 : j]
}

func labelForCompletion(cleaned, filter string) string {
	label := trimLen(firstLine(cleaned))
	if filter != "" && !strings.HasPrefix(strings.ToLower(label), strings.ToLower(filter)) {
		return filter
	}
	return label
}

func (s *Server) fallbackCompletionItems(docStr string) []CompletionItem {
	return []CompletionItem{{
		Label:         "hexai-complete",
		Kind:          1,
		Detail:        "dummy completion",
		InsertText:    "hexai",
		SortText:      "9999",
		Documentation: docStr,
	}}
}