Visual Studio Code - Change the value of a specific integer with a math operation in a line across multiple pages

Viewed 644

Not sure if this is something that can be done through Visual Studio Code, but I thought I would check here before I go ahead and manually modify thousands of lines in my project.

Please see the example below to help explain the issue I'm facing.

I have multiple files spread across multiple folders with an identifying text (in this case, Bolts), with another identifier (in this case, quality) value which equals an integer that is different on each page. I would like to know if it's possible to replace these integers with the updated integers through a basic mathematical equation.

Page A:

{
   "key": "storage01/bolts",
   "quality": 53.12,
   "weight": 30
},
{
   "key": "storage01/cogs",
   "quality": 39.17,
   "weight": 29
}

Page B:

{
   "key": "storage02/bolts",
   "quality": 18.9,
   "weight": 30
},
{
   "key": "storage02/cogs",
   "quality": 76.2,
   "weight": 29
}

I would like to change both numbers of the quality by multiplying them by 0.29 so the results would end up like this ..

Page A:

{
   "key": "storage01/bolts",
   "quality": 15.4048,
   "weight": 30
},
{
   "key": "storage01/cogs",
   "quality": 39.17,
   "weight": 29
}

Page B:

{
   "key": "storage02/bolts",
   "quality": 5.481,
   "weight": 30
},
{
   "key": "storage02/cogs",
   "quality": 76.2,
   "weight": 29
}
2 Answers

With the help of map-replace.js and one other extension Search Editor: Apply Changes you can do this pretty easily. Thanks to @rioV8 for the first extension, its replacement code and the second regex.

The idea is to do a search across files in a Search Editor, map-replace those values in that editor and then save those changes to all the underlying files.

(1) Open a new Search Editor (from command palette or make a keybinding to the command) or do a Search in the Panel and then choose Open in Editor.

(2) Do a regex search for \{\s*.*bolts[\w\W]*?\}

(3) Now in the Search Editor tab do a Find with (?<="quality":\s*)[\d.]+

(3) Alt+Enter to select all the matches (you may have to click in the editor first).

(4) Run Map and Replace Selection with JavaScript Function with (v, i) => `${(Number(v)*0.29).toFixed(2)} to make the desired changes. Set toFixed() to what you want.

(5) Run Apply Search Editor changes to worksapce from Search Editor: Apply Changes extension

and your changes will be saved within all the files. Pretty neat to use these extensions and the Search Editor functionality together - yours is a good use case (as long as the initial regex across files isn't super difficult to get right).

(6) Save all



Demo (with simplified replace since I can't seem to paste into the input box):

map-replace with apply all changes demo

You can use the extension map-replace-js

You select the numbers with a regex (?<="quality": )[\d.]+

Use ShiftCtrl+L to select all occurrences in the file.

Then call the Map and Replace Selection with JavaScript Function command and use the following expression to convert the numbers

(v, i) => `${(Number(v)*0.29).toFixed(2)}`

You have to do this in every file

Related