Home > database >  Skript - Get the body of the code component
Skript - Get the body of the code component

Time:02-14

I'm working on an extension for vs code that adds support for Skript syntax.

I'm stuck on that I can't select the body of the code block. In total, there are several of them in the Skript syntax (commands, functions, events, and a few more). Each block starts with a zero indent (it's indent sensitive). I have provided an example Skript syntax below.

on join:
    set the player's gamemode to spectator
    join_player(player)

command /join [<player>]:
    trigger:
        if arg-1 is not set:
            join_player(player)
        else:
            player has permission "op"
            join_player(arg-1)

function reset_arena():
    set all blocks within {loc1} and {loc2} to snow block

The original author of this extension used this construction. But it doesn't work with an odd number (\r\n|\r|\n). Also defines the last line of the document as not related to the last component.

if (search = component.match(/^(?<component>(command\s?(?<head>[^\:]*))\:?(.*)(?<body>((\r\n|\r|\n)([^a-zA-Z][^\r\n]*)?)*))/i)?.groups) {
    return this._createCommand(skDocument, range, search.component, search.head, search.body);  
}

Please tell me how to correctly make a regex so that the body of the component is selected. Thanks a lot

CodePudding user response:

I believe you need something like this:

(?<component>^\w   (?<head>.*)\n(?<body>(?:[\s\S](?!\n\w))*))

It will provide 3 groups for each match:

  1. component
  2. head
  3. body

You can have a better idea of how it works and make any modifications you may need in the following link: https://regex101.com/r/0VaKhR/2

  • Related