Sublime Forum

Chained (Piped?) Snippet REGEX substitutions?

#1

Is it possible to chain REGEX substitutions in a snippet?

It’s a feature I want in general, but specifically, I am trying to get my snippet to auto-generate the package in a Scala file that uses SBT. Currently, I have:

package ${TM_FILEPATH/.+src\/main\/scala\/(.+)\/.+\.scala$/$1//\//\./g}

The missing part is the translation of slashes to dots. I tried a nesting, but that seems to only work for placeholders.

Thanks for your time.

2 Likes

#2

Sorry to just “me too” on this, but I’m curious about the same thing.

0 Likes

#3

I’m not certain what your trying to match/replace, but have a look in this thread as it may be related.

0 Likes

#4

Has anyone found a solution for this? This is driving me crazy!

0 Likes

#5

I know this is old but any news about this?

I’m trying to build a snippet which transforms a testcase file path to PHP namespace but only if the path has a /Tests component. So the regex first matches the filepath without the filename, but then I need to change all / to .

So I need to chain the substitution so that I can match&substitute all I got in backreference \1

What I have now is (I’m omitting the full body of the snippet and instead just place the namespace statement):

namespace ${1:${TM_FILEPATH/.+?(?:((?:Tests)(?:\/.*)?)\/)?(?:[^\/]+)\.php$/\1/g}};

What I need would be:

namespace ${1:${${TM_FILEPATH/.+?(?:((?:Tests)(?:\/.*)?)\/)?(?:[^\/]+)\.php$/\1/g}/\//\\/g}};

Is it possible to do this (maybe the syntax could be different)

0 Likes

#6

@axelitus can you please give a “what I have” and “what I want” example. AFAIK you can’t nest replacements, but it may be possible to archive it anyway.

1 Like

#7

Hi @r-stein, thanks for your response. I’ve trying to do it without chaining, but as of right now I haven’t had any success.

What I have is basically $TM_FILEPATH, which could be for example: /home/axel/projects/example/src/tests/feature/awesome/MyObjectTest.php

What I would like is to end with a file that has:

<?php

namespace Tests\Feature\Awesome;

class MyObjectTest extends TestCase
{
    |
}

I simulated the cursor placement with a pipe. Also if there’s no “tests/” folder in the path, the namespace should just be “Tests”.

0 Likes

#8

I got it finally! I ended up doing this:

<snippet>
  <content><![CDATA[
<?php

namespace Tests${1:${TM_FILEPATH/(?:^.+Tests\/(?:[^\/]+\.php$)?|\G(?:([^\/]+)\/)|.+[^\/]+\.php$)/?1:\\\1:/ig}};

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ${TM_FILENAME/(\w+)\.php/\1/} extends TestCase
{
    ${0}
}

]]></content>
  <tabTrigger>testcase</tabTrigger>
  <description>New PHP TestCase scaffold</description>
  <scope></scope>
</snippet>

Thanks @r-stein for clarifying that chaining was not possible so I could take another approach to this!

This snippet is “specifically” for how Laravel structures its fodlers, but it can be customized to fit any needs. This even can be modified to apply the App namespace instead of the Tests namespace to make classes inside your App folder (just rename Tests to App wherever you find it in the namespace declaration).

2 Likes