Skip to content

Commit

Permalink
sp_help command added
Browse files Browse the repository at this point in the history
  • Loading branch information
vladk1973 authored Sep 6, 2021
1 parent 5650fe9 commit 17f8805
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 83 deletions.
147 changes: 147 additions & 0 deletions AlignStringsUnit.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
unit AlignStringsUnit;

interface

uses
Winapi.Windows, System.SysUtils, System.Classes, System.Math, ConstUnit;


type

TAlignStrings = class(TStringList)
private
procedure AlignDeclarations(const StartRow: Integer);
procedure AlignVars(const StartRow: Integer);
procedure AlignFunction(const StartRow: Integer; KeyWord: string);
public
procedure AlignFrom(const StartRow: Integer; KeyWord: string);
end;

implementation

{ TAlignStrings }

procedure TAlignStrings.AlignFrom(const StartRow: Integer; KeyWord: string);
begin
if KeyWord = cnstAlignStartWords[0] then AlignDeclarations(StartRow)
else
if KeyWord = cnstAlignStartWords[3] then
AlignVars(StartRow)
else
AlignFunction(StartRow,KeyWord);
end;

procedure TAlignStrings.AlignDeclarations(const StartRow: Integer);
var
iStartPos, iMax, iLen, iLenTotal, i: Integer;
Line, S, Variables: string;
begin
iMax := 0;
iStartPos := Length(cnstAlignStartWords[0]) + 4;
for i := StartRow to Count-1 do
begin
Line := String(WholeWord(AnsiString(Strings[i]),1));
iLen := Line.Length;
if (iLen>0) and (Line[1] = '@') then
iMax := Max(iMax,iLen);
end;

if iMax > 0 then
begin
iLenTotal := 0;
Variables := '';
for i := StartRow to Count-1 do
begin
if i = StartRow then
Strings[i] := StringReplace(Strings[i],cnstAlignStartWords[0],'',[rfIgnoreCase]);

S := Strings[i];

Line := String(WholeWord(AnsiString(S),1));
iLen := Line.Length;
if (iLen>0) and (Line[1] = '@') then
begin
if Pos(Line+',',Variables) = 0 then
begin
Variables := Variables + Line + ',';

S := StringReplace(RemoveComments(S),',','',[]);
S := StringReplace(S,Line,'',[rfIgnoreCase]).Trim;
Strings[i] := Line.PadRight(iMax).PadLeft(iStartPos+iMax-1) + ' ' + S;
iLenTotal := Max(iLenTotal,Strings[i].Length);
end
else
if (Pos('--',Strings[i]) = 0) then
Strings[i] := '--' + Strings[i];
end;
end;

if iLenTotal > 0 then
begin
for i := StartRow to Count-1 do
begin
if i = StartRow then
begin
Strings[i] := ' ' + cnstAlignStartWords[0] + ' ' + Strings[i].TrimLeft;
end;

if (Pos('@',Strings[i]) > 0) and (Pos('--',Strings[i]) = 0) then
Strings[i] := Strings[i].PadRight(iLenTotal) + ',';
end;

for i := Count-1 downto StartRow do
if (Pos('@',Strings[i]) > 0) and (Pos('--',Strings[i]) = 0) then
begin
Strings[i] := StringReplace(Strings[i],',','',[]);
Break;
end;
end;

end;
end;

procedure TAlignStrings.AlignFunction(const StartRow: Integer; KeyWord: string);
begin
if StartRow+1 < Count then
AlignVars(StartRow+1);
end;

procedure TAlignStrings.AlignVars(const StartRow: Integer);
var
iStartPos, iMax, iLen, i: Integer;
Line, S: string;
begin
iMax := 0;
iStartPos := Pos(cnstAlignStartWords[3],Strings[StartRow]);

for i := StartRow to Count-1 do
begin
Line := String(WholeWord(AnsiString(Strings[i]),1));
iLen := Line.Length;
if (iLen>0) and (Line[1] = '@') then
iMax := Max(iMax,iLen);
end;

if iMax > 0 then
begin
for i := StartRow to Count-1 do
begin
S := Strings[i];

Line := String(WholeWord(AnsiString(S),1));
iLen := Line.Length;
if (iLen>0) and (Line[1] = '@') then
begin
S := StringReplace(S,Line,'',[rfIgnoreCase]).Trim;
iLen := Pos('=',S);
if iLen = 1 then
Strings[i] := Line.PadRight(iMax).PadLeft(iStartPos+iMax-1) + ' = ' + Copy(S,iLen+1,MaxInt).Trim
else
Strings[i] := Line.PadRight(iMax).PadLeft(iStartPos+iMax-1) + ' ' + S;

end;
end;
end;
end;

end.
8 changes: 8 additions & 0 deletions ConstUnit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface
TModalResultArray = array[False..True] of TModalResult;
TGarbageArray = array[0..9] of string;
TBeginProcArray = array[0..3] of string;
TBeginAlignArray = array[0..3] of string;

TShowMode = (shSql,shPr,shCI);

TBdType = (bdMSSQL, bdSybase, bdODBC);
Expand Down Expand Up @@ -79,6 +81,12 @@ interface
'#define '}
);

cnstAlignStartWords: TBeginAlignArray = (
'declare',
'execute',
'exec' ,
'@'
);

cnstShowPlanXML_ON = 'SET SHOWPLAN_XML ON';
cnstShowPlanXML_OFF = 'SET SHOWPLAN_XML OFF';
Expand Down
93 changes: 78 additions & 15 deletions Easy.diaplugin.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface
Winapi.Windows, Winapi.Messages, System.SysUtils,
Vcl.Dialogs, Vcl.Forms, Vcl.Controls,
System.Classes, Vcl.ComCtrls, SciSupport, System.Contnrs, System.Math,
NppPlugin, ConstUnit, logFormUnit, prUnit, lookupProcUnit;
NppPlugin, ConstUnit, logFormUnit, prUnit, lookupProcUnit, AlignStringsUnit;

type

Expand All @@ -25,9 +25,7 @@ TdiaPlugin = class(TNppPlugin)
procedure DoNppnToolbarModification; override;
procedure DoNppnCharAdded(const ASCIIKey: Integer); override;
procedure DoNppnModified(sn: PSCNotification); override;
{$IFDEF WIN64}
procedure DoNppnUpdateAutoSelection(P: PAnsiChar); override;
{$ENDIF}

procedure ShowAutocompletion(const TableName: string; Indx: TStringList);
procedure ShowProcedureList;
Expand All @@ -41,6 +39,7 @@ TdiaPlugin = class(TNppPlugin)
procedure FuncReplaceProcParams;
procedure FuncSP_HELP;
procedure FuncSP_HELPINDEX;
procedure FuncAlignCode;
end;

var
Expand Down Expand Up @@ -113,6 +112,11 @@ procedure f_SP_HELPINDEX; cdecl;
Npp.FuncSP_HELPINDEX;
end;

procedure f_FuncAlignCode; cdecl;
begin
Npp.FuncAlignCode;
end;



procedure _FuncLForm; cdecl;
Expand Down Expand Up @@ -149,6 +153,8 @@ constructor TdiaPlugin.Create;
AddFuncItem('M_LOG_TABLE_REQ' ,f_M_LOG_TABLE_REQ);
AddFuncItem('-', nil );
AddFuncItem('Ïàðàìåòðèçàöèÿ ïðîöåäóðû' ,f_REPLACEPROCPARAMS);
AddFuncItem('Âûðîâíÿòü áëîê SQL êîäà' ,f_FuncAlignCode);


sk.IsCtrl := true; sk.IsAlt := false; sk.IsShift := false;
sk.Key := 123; // F12
Expand Down Expand Up @@ -290,6 +296,49 @@ procedure TdiaPlugin.DoNppnToolbarModification;
Npp_Send(NPPM_ADDTOOLBARICON, WPARAM(self.CmdIdFromDlgId(3)), LPARAM(@tb));
end;

procedure TdiaPlugin.FuncAlignCode;
function GetRightWord(S: string): string;
begin
Result := trim(S);
if Pos('=',Result) = 1 then
Result := '= ' + trim(Copy(Result,2,MaxInt));
end;
var
S: AnsiString;
Strings: TAlignStrings;
i,j,iPos: Integer;
begin
S := SelectedText;

Strings := TAlignStrings.Create;
try
Strings.Text := S;
for i := 0 to Strings.Count-1 do
begin
S := LowerCase(RemoveComments(Strings[i]));
iPos := 0;
for j := Low(cnstAlignStartWords) to High(cnstAlignStartWords) do
begin
iPos := Pos(cnstAlignStartWords[j],S);
if iPos > 0 then
begin
Strings.AlignFrom(i,cnstAlignStartWords[j]);
Break;
end;
end;
if iPos > 0 then Break;
end;

if (iPos > 0) then
begin
S := UTF8Encode(Strings.Text);
Sci_Send(SCI_REPLACESEL, 0, LPARAM(PAnsiChar(S)));
end;
finally
Strings.Free;
end;
end;

procedure TdiaPlugin.FuncExecSQL;
var S: string;
N: integer;
Expand Down Expand Up @@ -497,13 +546,19 @@ procedure TdiaPlugin.FuncReplaceProcParams;
iPos1:= Pos('!',Strings[i]); //Ýòî ïàðàìåòðû â DFM ôîðìå
cPos := Pos('''%',Strings[i]);
cPos1:= Pos('!''',Strings[i]); //Ýòî ïàðàìåòðû â êàâû÷êàõ
if cPos = 0 then
cPos := Pos('"%',Strings[i]);
if cPos1 = 0 then
cPos1:= Pos('!"',Strings[i]); //Ýòî ïàðàìåòðû â êàâû÷êàõ

if (iPos > 0) and (iPos1 > iPos) then
begin
S0 := RemoveComments(Copy(Strings[i],1,iPos-1));
S0 := StringReplace(S0,' ','',[rfReplaceAll]);
S0 := StringReplace(S0,',','',[rfReplaceAll]);
S0 := StringReplace(S0,'=','',[rfReplaceAll]);
S0 := StringReplace(S0,'''','',[rfReplaceAll]);
S0 := StringReplace(S0,'"','',[rfReplaceAll]);
S0 := trim(S0);
if Pos('@',S0) = 1 then
begin
Expand Down Expand Up @@ -575,12 +630,9 @@ procedure TdiaPlugin.FuncSP_HELP;
S := SelectedText;
N := Length(S);

if N < 1 then
if N = 0 then
begin
S := trim(GetText);
N := Pos(' ',S);
if N > 0 then
S := Copy(S,1,N-1);
S := GetWord;
N := Length(S);
end;

Expand All @@ -595,12 +647,9 @@ procedure TdiaPlugin.FuncSP_HELPINDEX;
S := SelectedText;
N := Length(S);

if N < 1 then
if N = 0 then
begin
S := trim(GetText);
N := Pos(' ',S);
if N > 0 then
S := Copy(S,1,N-1);
S := GetWord;
N := Length(S);
end;

Expand Down Expand Up @@ -628,13 +677,28 @@ function TdiaPlugin.GetTextRange(const Range: TCharacterRange): nppString;
end;
end;

{$IFDEF WIN64}
procedure TdiaPlugin.DoNppnUpdateAutoSelection(P: PAnsiChar);
var
S: AnsiString;
pS: PAnsiChar;
iLen: Integer;
begin
S := P;
if Length(S) = 0 then
begin
GetMem(pS,255);
try
iLen := Sci_Send(SCI_AUTOCGETCURRENTTEXT,0,LPARAM(pS));
if iLen > 0 then
begin
SetLength(S,iLen);
StrLCopy(PAnsiChar(S),pS,iLen);
end;
finally
FreeMem(pS);
end;
end;

if (Length(S)>0) and (S[1]= 'X') then
if (Pos('(',S) > 0) and (Pos(')',S) = Length(S)) then
begin
Expand All @@ -650,6 +714,5 @@ procedure TdiaPlugin.DoNppnUpdateAutoSelection(P: PAnsiChar);
end;
end;
end;
{$ENDIF}

end.
Loading

0 comments on commit 17f8805

Please sign in to comment.