*** This is not about passing arguments or using arrays or templates or building classes. It's about text editing. ***
Say I've written a function which manipulates some variables. Now I want a second function to do basically the same thing but with different variables - so I copy/paste the original function's text in function two's body. Is there any way to fence off an area of text and do one or more 'change all occurrences' inside that fence without affecting unfenced text? Does such a feature(?) even exist? See example sketch below.
void setup() {
// put your setup code here, to run once:
}
void loop() {
static int foo;
func1(foo);
func2(foo);
}
void func1(int bar1) {
int bar1Multiplier = 1;
bar1 = bar1 * bar1Multiplier;
}
//
void func2(int bar1) {
int bar1Mulitplier = 1 // How to change all instances of 'bar1' in body of func2
static int bar1Multiplier; // to 'bar2' and leave all 'bar1's in func1 unchanged?
bar1 = bar1 * bar1Multiplier;
}
Do you mean "refactoring" ? It's a feature available in IDEs like VS Code, I don't know if the Arduino IDE support this, I haven't used it since a long time.
Why? The bar1 argument to func2 is completely, totally different from the bar1 argument of func1. In each functions, bar1 is a local variable, and ceases to exist outside the function.
As for the text editing, you can use an external editor with the IDE. I know it can be done with vi on a linux system, but that is not what most people will be using.
Hi @dougp. You didn't specify a development tool so I'll provide instructions for Arduino IDE 2.x.
In the example you provided, it seems that you want to do a replace of all occurrences of the text (e.g., bar1Mulitplier -> foo1Mulitplier, bar1 -> foo1) rather than only renaming a specific symbol alone. In this case a basic brute force text replacement will be fastest:
Select the text in which you want to perform the replacement.
Select Edit > Find from the Arduino IDE menus.
The "Find" interface will open at the top right corner of the editor panel.
Select the icon that looks like three horizontal lines ("Find in Selection") on the right side of the "Find" interface.
Type the text you want to replace into the "Find" field.
Click the ❯ icon on the left side of the "Find" interface.
The "Replace" interface will expand.
Type the replacement text into the "Replace" field.
Click the "Replace All" icon to the right side of the "Replace" field.
In other cases, you might want to rename only a code symbol rather than doing a brute force text replacement. Arduino IDE 2.x has a language server that gives it an understanding of the sketch program, which makes such a thing possible:
Right click on the symbol you want to rename in the editor panel.
A context menu will open.
Select "Rename Symbol" from the context menu.
A renaming interface will open in the editor.
Type the new name into the field in the interface.
Press the Enter key.
You will find that the renaming is correctly only applied to the selected symbol, leaving other occurrences of text and other symbols of the same name (e.g., the bar1 variable in the func1 function) unchanged.
void setup() {
// put your setup code here, to run once:
}
void loop() {
static int foo;
func1(foo);
func2(foo);
}
void func1(int bar1) {
int bar1Multiplier = 1;
bar1 = bar1 * bar1Multiplier;
}
//
void func2(int bar1) {
int bar2Mulitplier = 1 // See how all 'bar1' has been changed to 'bar2'
static int bar2Multiplier; // in the body of the function?
bar2 = bar2 * bar2Multiplier;
}
No. I want to replace the text bar1 with text bar2 in the second function only using a kind of 'change all occurrences' feature - if such exists - and not affect the bar1 text in the first function.
The goal is to duplicate a function and change the names of variables without a piecemeal technique and without changing every variable in the document..
Not at first. I looked at your example and thought you were explaining something else. I claim befuddlement. I made a second pass following the instructions given step by step and... viola!