Search replace complex within each class 在每个类内部搜索替换复杂内容

huangapple go评论56阅读模式
英文:

vim: Search replace complex within each class

问题

我在一个文件中有许多嵌套类。每个类类似于这个,但在ProtoMember(ID)中有不同的ID。

public class ApiInputs 
{
    [ProtoMember(316)]
    [JsonPropertyName("Meta")] 
    public Meta Meta { get; set; }

    [ProtoMember(317)]
    [JsonPropertyName("Settings")] 
    public Settings? Settings { get; set; }

    [ProtoMember(318)]
    [JsonPropertyName("Weight")] 
    public Weight? Weight { get; set; }
}

我想替换每个类中的ID,每次从1开始,而不是从316开始(在这个类中)。我可以用%%%%全局替换所有内容,然后使用类似以下的vim命令进行替换,如果我只有一个类的话,我想要的就是这样。但是,我需要在每个类中重置计数器。

:let i=1 | g/%%%%/s//\=i/ | let i = i+1

我尝试过几个(中级技能)vim命令和宏,并成功地几次挂起了我的vim编辑器。那么,如何告诉宏或命令行在每个类中进行这种更改,每次重新启动计数器。

有没有不需要hack的方法?还是使用awk更好?

英文:

I have many nested classes in a file. Each similar to this, but with different IDs in the ProtoMember(ID).

public class ApiInputs 
{
    [ProtoMember(316)]
    [JsonPropertyName("Meta")] 
    public Meta Meta { get; set; }

    [ProtoMember(317)]
    [JsonPropertyName("Settings")] 
    public Settings? Settings { get; set; }

    [ProtoMember(318)]
    [JsonPropertyName("Weight")] 
    public Weight? Weight { get; set; }
}

I want to replace the IDs within each class starting each time at 1 instead of 316 (in this class). I can global replace all with "%%%%" and then use a vim command like the following to replace like I want if I only had one class. But, I need to reset the counter in each class.

:let i=1 | g/%%%%/s//\=i/ | let i = i+1

I've tried several (intermediate skill) vim commands and macros and successfully hung both of my vim editors a few times. So, how can I tell the macro or the command line to make this change within each class, restarting the counter each time.

Possible without a hack? or better to awk?

and

答案1

得分: 0

以下是您要翻译的内容:

"The trick would be to increment the counter inside the substitute expression using silent execute() and another execute() to check if we entered a new class to reset the counter:"
"将计数器递增放在替代表达式内部,使用静默的 execute() 和另一个 execute() 来检查是否进入了一个新类以重置计数器:"

:let i = 1 | %s/\(public class\_.\{-}\)\?ProtoMember(\zs\d\+\ze)/\=execute('if !empty(submatch(1)) | let i = 1 | endif') .. i .. execute('let i = i + 1')/

将会得到:

public class ApiInputs 
{
    [ProtoMember(1)]
    [JsonPropertyName("Meta")] 
    public Meta Meta { get; set; }

    [ProtoMember(2)]
    [JsonPropertyName("Settings")] 
    public Settings? Settings { get; set; }

    [ProtoMember(3)]
    [JsonPropertyName("Weight")] 
    public Weight? Weight { get; set; }
}

public class ApiOutputs 
{
    [ProtoMember(1)]
    [JsonPropertyName("Meta")] 
    public Meta Meta { get; set; }

    [ProtoMember(2)]
    [JsonPropertyName("Settings")] 
    public Settings? Settings { get; set; }

    [ProtoMember(3)]
    [JsonPropertyName("Weight")] 
    public Weight? Weight { get; set; }
}
英文:

The trick would be to increment the counter inside the substitute expression using silent execute() and another execute() to check if we entered a new class to reset the counter:

:let i = 1 | %s/\(public class\_.\{-}\)\?ProtoMember(\zs\d\+\ze)/\=execute('if !empty(submatch(1)) | let i = 1 | endif') .. i .. execute('let i = i + 1')/

will give:

public class ApiInputs 
{
    [ProtoMember(1)]
    [JsonPropertyName("Meta")] 
    public Meta Meta { get; set; }

    [ProtoMember(2)]
    [JsonPropertyName("Settings")] 
    public Settings? Settings { get; set; }

    [ProtoMember(3)]
    [JsonPropertyName("Weight")] 
    public Weight? Weight { get; set; }
}

public class ApiOutputs 
{
    [ProtoMember(1)]
    [JsonPropertyName("Meta")] 
    public Meta Meta { get; set; }

    [ProtoMember(2)]
    [JsonPropertyName("Settings")] 
    public Settings? Settings { get; set; }

    [ProtoMember(3)]
    [JsonPropertyName("Weight")] 
    public Weight? Weight { get; set; }
}

huangapple
  • 本文由 发表于 2023年6月15日 02:31:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定