context-mentions.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. - **Regex Breakdown**:
  3. 1. **Pattern Components**:
  4. - The regex is built from multiple patterns joined with OR (|) operators
  5. - Each pattern handles a specific type of mention:
  6. - Unix/Linux paths
  7. - Windows paths with drive letters
  8. - Windows relative paths
  9. - Windows network shares
  10. - URLs with protocols
  11. - Git commit hashes
  12. - Special keywords (problems, git-changes, terminal)
  13. 2. **Unix Path Pattern**:
  14. - `(?:\\/|^)`: Starts with a forward slash or beginning of line
  15. - `(?:[^\\/\\s\\\\]|\\\\[ \\t])+`: Path segment that can include escaped spaces
  16. - `(?:\\/(?:[^\\/\\s\\\\]|\\\\[ \\t])+)*`: Additional path segments after slashes
  17. - `\\/?`: Optional trailing slash
  18. 3. **Windows Path Pattern**:
  19. - `[A-Za-z]:\\\\`: Drive letter followed by colon and double backslash
  20. - `(?:(?:[^\\\\\\s/]+|\\/[ ])+`: Path segment that can include spaces escaped with forward slash
  21. - `(?:\\\\(?:[^\\\\\\s/]+|\\/[ ])+)*)?`: Additional path segments after backslashes
  22. 4. **Windows Relative Path Pattern**:
  23. - `(?:\\.{0,2}|[^\\\\\\s/]+)`: Path prefix that can be:
  24. - Current directory (.)
  25. - Parent directory (..)
  26. - Any directory name not containing spaces, backslashes, or forward slashes
  27. - `\\\\`: Backslash separator
  28. - `(?:[^\\\\\\s/]+|\\\\[ \\t]|\\/[ ])+`: Path segment that can include spaces escaped with backslash or forward slash
  29. - `(?:\\\\(?:[^\\\\\\s/]+|\\\\[ \\t]|\\/[ ])+)*`: Additional path segments after backslashes
  30. - `\\\\?`: Optional trailing backslash
  31. 5. **Network Share Pattern**:
  32. - `\\\\\\\\`: Double backslash (escaped) to start network path
  33. - `[^\\\\\\s]+`: Server name
  34. - `(?:\\\\(?:[^\\\\\\s/]+|\\/[ ])+)*`: Share name and additional path components
  35. - `(?:\\\\)?`: Optional trailing backslash
  36. 6. **URL Pattern**:
  37. - `\\w+:\/\/`: Protocol (http://, https://, etc.)
  38. - `[^\\s]+`: Rest of the URL (non-whitespace characters)
  39. 7. **Git Hash Pattern**:
  40. - `[a-zA-Z0-9]{7,40}\\b`: 7-40 alphanumeric characters followed by word boundary
  41. 8. **Special Keywords Pattern**:
  42. - `problems\\b`, `git-changes\\b`, `terminal\\b`: Exact word matches with word boundaries
  43. 9. **Termination Logic**:
  44. - `(?=[.,;:!?]?(?=[\\s\\r\\n]|$))`: Positive lookahead that:
  45. - Allows an optional punctuation mark after the mention
  46. - Ensures the mention (and optional punctuation) is followed by whitespace or end of string
  47. - **Behavior Summary**:
  48. - Matches @-prefixed mentions
  49. - Handles different path formats across operating systems
  50. - Supports escaped spaces in paths using OS-appropriate conventions
  51. - Cleanly terminates at whitespace or end of string
  52. - Excludes trailing punctuation from the match
  53. - Creates both single-match and global-match regex objects
  54. */
  55. const mentionPatterns = [
  56. // Unix paths with escaped spaces using backslash
  57. "(?:\\/|^)(?:[^\\/\\s\\\\]|\\\\[ \\t])+(?:\\/(?:[^\\/\\s\\\\]|\\\\[ \\t])+)*\\/?",
  58. // Windows paths with drive letters (C:\path) with support for escaped spaces using forward slash
  59. "[A-Za-z]:\\\\(?:(?:[^\\\\\\s/]+|\\/[ ])+(?:\\\\(?:[^\\\\\\s/]+|\\/[ ])+)*)?",
  60. // Windows relative paths (folder\file or .\folder\file) with support for escaped spaces
  61. "(?:\\.{0,2}|[^\\\\\\s/]+)\\\\(?:[^\\\\\\s/]+|\\\\[ \\t]|\\/[ ])+(?:\\\\(?:[^\\\\\\s/]+|\\\\[ \\t]|\\/[ ])+)*\\\\?",
  62. // Windows network shares (\\server\share) with support for escaped spaces using forward slash
  63. "\\\\\\\\[^\\\\\\s]+(?:\\\\(?:[^\\\\\\s/]+|\\/[ ])+)*(?:\\\\)?",
  64. // URLs with protocols (http://, https://, etc.)
  65. "\\w+:\/\/[^\\s]+",
  66. // Git hashes (7-40 alphanumeric characters)
  67. "[a-zA-Z0-9]{7,40}\\b",
  68. // Special keywords
  69. "problems\\b",
  70. "git-changes\\b",
  71. "terminal\\b",
  72. ]
  73. // Build the full regex pattern by joining the patterns with OR operator
  74. const mentionRegexPattern = `@(${mentionPatterns.join("|")})(?=[.,;:!?]?(?=[\\s\\r\\n]|$))`
  75. export const mentionRegex = new RegExp(mentionRegexPattern)
  76. export const mentionRegexGlobal = new RegExp(mentionRegexPattern, "g")
  77. export interface MentionSuggestion {
  78. type: "file" | "folder" | "git" | "problems"
  79. label: string
  80. description?: string
  81. value: string
  82. icon?: string
  83. }
  84. export interface GitMentionSuggestion extends MentionSuggestion {
  85. type: "git"
  86. hash: string
  87. shortHash: string
  88. subject: string
  89. author: string
  90. date: string
  91. }
  92. export function formatGitSuggestion(commit: {
  93. hash: string
  94. shortHash: string
  95. subject: string
  96. author: string
  97. date: string
  98. }): GitMentionSuggestion {
  99. return {
  100. type: "git",
  101. label: commit.subject,
  102. description: `${commit.shortHash} by ${commit.author} on ${commit.date}`,
  103. value: commit.hash,
  104. icon: "$(git-commit)", // VSCode git commit icon
  105. hash: commit.hash,
  106. shortHash: commit.shortHash,
  107. subject: commit.subject,
  108. author: commit.author,
  109. date: commit.date,
  110. }
  111. }