ryu1kn / vscode-text-marker

Visual Studio Code Extension. Select text in your code and mark all matches. The marking colour is configurable
https://marketplace.visualstudio.com/items?itemName=ryu1kn.text-marker
MIT License
87 stars 17 forks source link

Add support for recursive regex #36

Open jsve opened 5 years ago

jsve commented 5 years ago

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)

ryu1kn commented 5 years ago

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

jsve commented 5 years ago

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
      }
    },
ryu1kn commented 5 years ago

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)

jsve commented 5 years ago

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.

cmarabate commented 2 years ago

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