vscode-text-marker icon indicating copy to clipboard operation
vscode-text-marker copied to clipboard

Add support for recursive regex

Open jsve opened this issue 6 years ago • 5 comments

Recursive regex would be real useful when working with nested JSON objects.

(sorry about the short feature request, but I think you get the idea)

jsve avatar May 17 '19 13:05 jsve

Hi @jsve , sounds good. Do you want to provide sample input (text and a recursive pattern) and the expected highlighted part in the text?

ryu1kn avatar May 18 '19 12:05 ryu1kn

My use case is currently for CloudFormation templates. Each resource in the template has a type, which contains "Lambda", "ApiGateway", "Role" or some other resource. When the file gets long (3000+ rows) it would be useful to be able to colour-code each resource type. My idea is to write a regex for your plugin that colors the entire resource based on this "type" field. The recursive feature is the best way I have found to check which is the last closing bracket in the object. See example CloudFormation template below:

"GreetingLambda": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "ZipFile": { "Fn::Join": ["\n", [
            "'use strict';",
            "",
            "// Greeter Lambda",
            "exports.handler = (event, context, callback) => {",
            "  console.log('Event:', JSON.stringify(event));",
            "  const name = event.name || 'World';",
            "  const response = {greeting: `Hello, ${name}!`};",
            "  callback(null, response);",
            "};"
          ]]}
        },
        "Description": "A greeting function",
        "FunctionName": "GreetingLambda",
        "Handler": "index.handler",
        "Role": { "Fn::GetAtt": ["LambdaExecutionRole", "Arn"]},
        "Runtime": "nodejs4.3"
      }
    },

    "LambdaExecutionRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [{
            "Effect": "Allow",
            "Principal": { "Service": ["lambda.amazonaws.com"] },
            "Action": ["sts:AssumeRole"]
          }]
        },
        "ManagedPolicyArns": ["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"]
      }
    },

    "GreetingApi": {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "Name": "Greeting API",
        "Description": "API used for Greeting requests",
        "FailOnWarnings": true
      }
    },

jsve avatar May 21 '19 08:05 jsve

Thanks @jsve for providing the use case. CloudFormation is one of my favourite services on AWS 😃

So you want to, for example, highlight the following string with a certain colour, don't you?

    {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "Name": "Greeting API",
        "Description": "API used for Greeting requests",
        "FailOnWarnings": true
      }
    }

Looks like XRegExp is a popular regex extension for js, but it doesn't seem to support regex recursion like (?R) in Perl or \g<0> in Ruby. Do you think we can still achieve what you want with XRegExp? (sorry I haven't played around this myself)

ryu1kn avatar May 21 '19 12:05 ryu1kn

Yes, that's what I'm trying to do.

I think the method matchRecursive (https://www.npmjs.com/package/xregexp#xregexpmatchrecursive) might be useful, but not sure how it would fit into the plugin. Adding support for "real" regex recursion is probably more user (developer) friendly.

jsve avatar May 21 '19 14:05 jsve

I need this as well to be able to use this regex in VSCode... https://regex101.com/r/tsqfP6/8/

cmarabate avatar Nov 11 '21 21:11 cmarabate