diff --git a/README.md b/README.md index 2f9c6cb..9ca2302 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Global and multiline options can be added for evaluation with a side-by-side doc ### 0.6.0 - Add support for `d` flag in TypeScript/JavaScript (PR by [@wszgrcy](https://github.com/wszgrcy)) +- Add Java support ### 0.5.0 diff --git a/package.json b/package.json index f2713e6..d69f468 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "regex", "displayName": "Regex Previewer", - "description": "Regex matches previewer for JavaScript, TypeScript, PHP and Haxe in Visual Studio Code.", + "description": "Regex matches previewer for JavaScript, TypeScript, PHP, Haxe and Java in Visual Studio Code.", "version": "0.6.0", "publisher": "chrmarti", "repository": { @@ -34,7 +34,8 @@ "onLanguage:typescriptreact", "onLanguage:vue", "onLanguage:php", - "onLanguage:haxe" + "onLanguage:haxe", + "onLanguage:java" ], "main": "./out/extension", "browser": "./out/extension", diff --git a/src/extension.ts b/src/extension.ts index 828350a..9f16e6f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -17,6 +17,7 @@ export function activate(context: vscode.ExtensionContext) { const regexRegex = /(^|\s|[()={},:?;])(\/((?:\\\/|\[[^\]]*\]|[^/])+)\/([gimuyd]*))(\s|[()={},:?;]|$)/g; const phpRegexRegex = /(^|\s|[()={},:?;])['|"](\/((?:\\\/|\[[^\]]*\]|[^/])+)\/([gimuy]*))['|"](\s|[()={},:?;]|$)/g; const haxeRegexRegex = /(^|\s|[()={},:?;])(~\/((?:\\\/|\[[^\]]*\]|[^/])+)\/([gimsu]*))(\s|[.()={},:?;]|$)/g; + const javaRegexRegex = /(^|\s|[()={},:?;]|[a-zA-Z_$][a-zA-Z0-9_$]*\.)(Pattern\.compile\s*\(\s*"([^"\\]*(\\.[^"\\]*)*)"\s*\)|(?:matches|replaceAll|replaceFirst|split)\s*\(\s*"([^"\\]*(\\.[^"\\]*)*)"\s*[,)])(\s|[()={},:?;]|$)/g; const regexHighlight = vscode.window.createTextEditorDecorationType({ backgroundColor: 'rgba(100,100,100,.35)' }); const matchHighlight = vscode.window.createTextEditorDecorationType({ backgroundColor: 'rgba(255,255,0,.35)' }); @@ -38,7 +39,7 @@ www.demo.com http://foo.co.uk/ https://marketplace.visualstudio.com/items?itemName=chrmarti.regex https://github.com/chrmarti/vscode-regex `; - const languages = ['javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'vue', 'php', 'haxe']; + const languages = ['javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'vue', 'php', 'haxe', 'java']; const decorators = new Map(); @@ -295,12 +296,27 @@ https://github.com/chrmarti/vscode-regex return haxeRegexRegex; } else if (languageId == 'php') { return phpRegexRegex; + } else if (languageId == 'java') { + return javaRegexRegex; } return regexRegex; } function createRegexMatch(document: vscode.TextDocument, line: number, match: RegExpExecArray) { - const regex = createRegex(match[3], match[4]); + let pattern: string; + let flags: string; + + if (document.languageId === 'java') { + // For Java, pattern is in match[3] or match[5], flags are empty + pattern = match[3] || match[5]; + flags = ''; + } else { + // For other languages, use the standard format + pattern = match[3]; + flags = match[4]; + } + + const regex = createRegex(pattern, flags); if (regex) { return { document: document,