IDE Window: Extract Procedure/es

From Lazarus wiki
Jump to navigationJump to search

Deutsch (de) English (en) español (es) suomi (fi) français (fr) русский (ru) slovenčina (sk) 中文(中国大陆)‎ (zh_CN)

   Resumen: "Extraer Procedimiento" toma las sentencias pascal seleccionadas y crea un nuevo procedimiento o método con ese código. Esta herramienta es útil para dividir procedimientos muy grandes, o para crear fácilmente un nuevo procedimiento a partir de algo de código.

 Ejemplo básico: <pascal> procedure HacerAlgo;

begin
  UsarHacerOtraCosa;
end;</pascal>

 Selecciona la línea "UsarHacerOtraCosa;" y utiliza Extraer Procedure de la opción Refactoring del menú contextual. Aparece un diálogo en el que se puede seleccionar el nombre y el tipo del procedimiento que se creará. En nuestro caso el resultado será este:

<pascal> procedure NuevoProcedimiento;

begin
  UsarHacerOtraCosa;
end;

procedure HacerAlgo;
begin
  NuevoProcedimiento;
end;

</pascal>

 Como se puede ver "NuevoProcedimiento" se crea con las sentencias seleccionadas dentro del su cuerpo y que la selección a sido reemplazada por una llamada a "NuevoProcedimiento".

Local Variables and Parameters

"Extract Proc" scans for used variables and automatically creates the parameter list and local variables. Example:

<pascal>

procedure TForm1.DoSomething(var Erni, Bert: integer);
var
  i: Integer; // Comment
begin
  Erni:=Erni+Bert;
  for i:=Erni to 5 do begin
  |
  end;
end;

</pascal>

Select the for loop and create a new Procedure "NewProc". The local variable i is only used in the selection, so it will be moved to the new procedure. Erni is also used in the remaining code, so it will become a parameter.

Result:

<pascal>

procedure NewProc(const Erni: integer);
var
  i: Integer; // Comment
begin
  for i:=Erni to 5 do begin
  |
  end;
end;

procedure TForm1.DoSomething(var Erni, Bert: integer);
begin
  Erni:=Erni+Bert;
  NewProc(Erni);
end;

</pascal>

You can see "i" was moved to the new procedure (Note: including its comment) and Erni.

Limitations:
Pascal is a very powerful language, so don't expect it will work with every code. Current limits/ToDos:

  • check if selection bounds on statement bounds
  • "with" statements