csharpdeeb25a2 | 1 | namespace CascadeIDE.Services; |
| 2 | |
| 3 | public static class CSharpCompletionPrefix |
| 4 | { |
| 5 | public static string Extract(string sourceText, int line1, int column1) |
| 6 | { |
| 7 | if (string.IsNullOrEmpty(sourceText) || line1 < 1 || column1 < 1) |
| 8 | return ""; |
| 9 | |
| 10 | var lineStart = 0; |
| 11 | var line = 1; |
| 12 | for (var i = 0; i < sourceText.Length && line < line1; i++) |
| 13 | { |
| 14 | if (sourceText[i] == '\n') |
| 15 | { |
| 16 | line++; |
| 17 | lineStart = i + 1; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | if (line != line1) |
| 22 | return ""; |
| 23 | |
| 24 | var lineEnd = sourceText.IndexOf('\n', lineStart); |
| 25 | if (lineEnd < 0) |
| 26 | lineEnd = sourceText.Length; |
| 27 | |
| 28 | var colIndex = Math.Min(column1 - 1, lineEnd - lineStart); |
| 29 | if (colIndex < 0) |
| 30 | return ""; |
| 31 | |
| 32 | var end = lineStart + colIndex; |
| 33 | var start = end; |
| 34 | while (start > lineStart && IsIdentifierPart(sourceText[start - 1])) |
| 35 | start--; |
| 36 | |
| 37 | return start < end ? sourceText[start..end] : ""; |
| 38 | } |
| 39 | |
| 40 | private static bool IsIdentifierPart(char c) => |
| 41 | char.IsLetterOrDigit(c) || c is '_' or '@'; |
| 42 | } |
| 43 | |
View only · write via MCP/CIDE