Autor Beitrag
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Mo 18.04.05 18:52 
hallo, ich möchte in einem Richedit die Kommentare highlighten, so wie in delphi oder hier im forum, jedoch funzt der code net so wie ich es will, hab ihn schon oft umgebaut, klappt aber irgendwie net :? :shock:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
procedure HighLightComments(re: TRichedit);
var
  i: integer;
  str: string;
  t1, t2, t3: boolean;
begin
  t1:= false;
  t2:= false;
  t3:= false;
  for i:= 1 to length(re.text) do
    begin
      str:= copy(re.text, i, 2);
      if not ((t1) or (t2) or (t3)) then //die klammer um die 3 Ausdrücke fehlten ^^
        begin
          if str = '//' then
            t1:= true;
          if str = '(*' then
            t2:= true;
          if str[1] = '{' then
            t3:= true;
          if (t1) or (t2) or (t3) then
            re.SelStart:= i - 1;
        end else
          begin
            if (t1) and (str = #13#10then
              begin
                t1:= false;
                re.SelLength:= i - re.selstart + 1;
                re.SelAttributes.Color:= clgreen;
                //re.SelAttributes.Style:= [fsitalic];
              end;
            if (t2) and (str = '*)'then
              begin
                t2:= false;
                re.SelLength:= i - re.SelStart + 1;
                re.SelAttributes.Color:= clgreen;
                //re.SelAttributes.Style:= [fsitalic];
              end;
            if (t3and (str[1] = '}'then //da stand vorher t2 ^^
              begin
                t3:= false;
                re.SelLength:= i - re.SelStart;
                re.SelAttributes.Color:= clgreen;
                //re.SelAttributes.Style:= [fsitalic];
              end;
          end;
    end;
end;


Zuletzt bearbeitet von F34r0fTh3D4rk am Mo 18.04.05 19:52, insgesamt 3-mal bearbeitet
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Mo 18.04.05 18:56 
kapier ich net, ich hab danach mal

ausblenden Delphi-Quelltext
1:
re.font.Color:= clblack;					


gemacht und jetzt stimmt es ??!!?!?!?!?!!! lol ? :lol:

aber {} und (* *) funzen immer noch net :? :shock:

Ganz komisch (screenshot) :shock:
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Mo 18.04.05 19:26 
Abgesehen vom Programmierstil und so: Schau dir die Zeilen 13, 39 und 42 nochmals genauer an.
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Mo 18.04.05 19:29 
was hast du gegen meinen stil ? ok wenn ichs noch opensource mache oder auch wenn mir lustig ist, mach ich noch leerzeilen rein, aber net imma :P

zu den 1. beiden fällt mir nischt ein, beim dritten habe ich soeben das +1 entfernt :shock: :? 8) :x :P :oops: :cry: :?:

aber der fehler ist der ausm screenie, ab dem 3. absatz geht garnischt mehr :?
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Mo 18.04.05 19:35 
Zwei Tips zum heutigen Rätsel:
Zu Zeile 13: Klammerfehler.
Zeile 39: Vergleiche 18-tes Zeichen der Zeilen 32 und 39.

;)
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Mo 18.04.05 19:37 
stimm dummer klammerfehler ^^ :lol:

Zitat:

Zeile 39: Vergleiche 18-tes Zeichen der Zeilen 32 und 39.


da find ich nischt :?

bis jetzt ist der fehler aber geblieben (normal ist ja praktisch nischt anders, außer bei t2 und t3)
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Mo 18.04.05 19:47 
Welches t3 auf Zeile 39?
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Mo 18.04.05 19:49 
ich meinte die klammern, aber funzt imm anoch net (wie auch :lol: )
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Mo 18.04.05 19:51 
aahaaa es war ein "str+c; strg+v"-Fehler ^^
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 19.04.05 13:37 
so, ich hab des jetzt soweit fertig, jedoch habe ich noch einige probleme:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  ReservedWords: array[0..75of string =
    ('and''array''as''asm''at''begin''case''class''const',
     'constructor''default''destructor''dispinterface''div''do',
     'downto''else''end''except''exports''file''finalization',
     'finally''for''function''goto''if''implementation''in',
     'inherited''initialization''inline''interface''is''label',
     'library''message''mod''nil''not''object''of''on''or',
     'out''packed''procedure''program''property''raise''read',
     'record''repeat''resourcestring''set''shl''shr''string',
     'stored''then''threadvar''to''try''type''unit''until',
     'uses''var''while''with''write''xor''private''protected',
     'public''published');
  AllowedChars: array[0..5of string =
    ('('')''['']'' ''');
  t, t1, t2, t3: boolean;

implementation

{$R *.dfm}

procedure HighlightReservedWords(re: Trichedit);
var
  i, j, k: integer;
  rword, str: string;
begin
  for i:= 1 to length(re.Text) do
    for j:= 0 to High(ReservedWords) do
      begin
        application.processmessages;
        rword:= ReservedWords[j];
        str:= Copy(re.text, i, Length(rword));
        if (str = rword) then
          begin
            re.SelStart:= i - 1;
            re.Sellength:= length(rword);
            re.SelAttributes.Color:= clnavy;
            re.SelAttributes.style:= [fsbold];
          end;
      end;
end;

procedure HighlightStrings(re: TRichedit);
var
  i: integer;
begin
  t:= false;
  for i:= 1 to length(re.text) do
    begin
      application.processmessages;
      if not (t) then
        begin
          if re.text[i] = '''' then
            begin
              t:= true;
              re.selstart:= i - 1;
            end;
        end else
          if (t) and (re.Text[i] = ''''then
            begin
              t:= false;
              re.SelLength:= i - re.selstart;
              re.SelAttributes.Color:= clblue;
              re.SelAttributes.Style:= [];
            end;
    end;
end;

procedure HighLightComments(re: TRichedit);
var
  i: integer;
  str: string;
begin
  t1:= false;
  t2:= false;
  t3:= false;
  for i:= 1 to length(re.text) do
    begin
      application.processmessages;
      str:= copy(re.text, i, 2);
      if not ((t1) or (t2) or (t3)) then
        begin
          if str = '//' then
            t1:= true;
          if str = '(*' then
            t2:= true;
          if str[1] = '{' then
            t3:= true;
          if ((t1) or (t2) or (t3)) then
            re.SelStart:= i - 1;
        end else
          begin
            if (t1) and (str = #13#10then
              begin
                t1:= false;
                re.SelLength:= i - re.selstart + 1;
                re.SelAttributes.Color:= clgreen;
                re.SelAttributes.Style:= [];
              end;
            if (t2) and (str = '*)'then
              begin
                t2:= false;
                re.SelLength:= i - re.SelStart + 1;
                re.SelAttributes.Color:= clgreen;
                re.SelAttributes.Style:= [];
              end;
            if (t3) and (str[1] = '}'then
              begin
                t3:= false;
                re.SelLength:= i - re.SelStart;
                re.SelAttributes.Color:= clgreen;
                re.SelAttributes.Style:= [];
              end;
          end;
    end;
end;

procedure SyntaxHighLighting(re: TRichEdit);
begin
  re.Font.Name:= 'arial';
  re.Font.Color:= clblack;
  HighlightReservedWords(re);
  HighlightStrings(re);
  HighlightComments(re);
  HighlightStrings(re); //Nur zu testzwecken !
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SyntaxHighLighting(Richedit1);
end;

end.


1) wenn sich kommentarzeichen innerhalb eines strings befinden, dann wird es nicht wie ein string behandelt sondern wie ein kommentar (umgekehrt wäre es änhlich)

2) Die Prozedure ist sehr langsam

3) Wenn sich ein Reserviertes Wort in einem anderen Wort befindet, wird dieses trotzdem gehighlighted (mögliche lösung: nur klammern und leerzeichen, vor bzw hinter dem wort zulassen)

zu punkt eins und 2 habe ich noch keine gescheite lösung, die geschwindigkeit ist mir auch (vorerst) egal 8)

zu punkt 3 dachte ich mir das so:
ausblenden Delphi-Quelltext
1:
if (str = rword) and (str[i - 1in ['('')''['']'' ''']) and (str[i + 1in ['('')''['']'' ''']) then					

des funzt aber net :? (hab auch schon andere sachen ausprobiert, funzen auch net (siehe variable allowed chars))

Wie siehts mit dem PHP code vom delphi forum aus ? ist doch im grunde das selbe ?