source: dev/trunk/abdev/ProjectEditor/CodeFormatter.cpp@ 159

Last change on this file since 159 was 24, checked in by dai_9181, 18 years ago

保存されていないドキュメントのタブに(*)をつける機能に対応。
MDITEXTEDITINFOをCMdiTextEditに変更。今後、オブジェクト指向化を進める。

File size: 5.0 KB
Line 
1#include "common.h"
2
3// インデント関連
4
5int IsIndentAdditionCommand_FromBuffer(char *buffer);
6int IsIndentDecreaseCommand_FromBuffer(char *buffer);
7
8char *CodeFormatter(char *lpszReadBuffer){
9 int WndNum;
10 WndNum=GetNowWndNum();
11
12 if(MdiInfo[WndNum].DocType!=WNDTYPE_BASIC){
13 goto NonFormater;
14 }
15
16 char *lpszTempReadBuffer;
17 lpszTempReadBuffer=(char *)HeapAlloc(hHeap,0,lstrlen(lpszReadBuffer)+1);
18 lstrcpy(lpszTempReadBuffer,lpszReadBuffer);
19
20 //CRLFをLFに変換
21 ChangeReturnCode(lpszTempReadBuffer);
22
23 DeleteComment(lpszTempReadBuffer);
24
25 //カッコの相互関係をチェック
26 if(!CheckParenthesis(lpszTempReadBuffer)){
27 HeapDefaultFree(lpszTempReadBuffer);
28 goto NonFormater;
29 }
30
31
32
33 ////////////////////////////////////
34 // インデントの初期TAB文字数を取得
35 ////////////////////////////////////
36
37 char *pBuf;
38 pBuf=MdiInfo[WndNum].pMdiTextEdit->buffer;
39
40 //カーソルバッファ位置を取得
41 int i,i2;
42 i=GetBufferIndexFromCaretPos(pBuf,
43 MdiInfo[WndNum].pMdiTextEdit->StartCaretPos.x,
44 MdiInfo[WndNum].pMdiTextEdit->StartCaretPos.y);
45 int StartX=MdiInfo[WndNum].pMdiTextEdit->StartCaretPos.x;
46
47 //現在の行のインデントをコピー(iFirstIndent_Speciallyにタブ数を格納)
48 int iFirstIndent_Specially=0;
49 for(i2=i;i2>0;i2--){
50 if(pBuf[i2-1]=='\r'&&pBuf[i2]=='\n'){
51
52 i=i2-1;
53
54 i2++;
55
56 int x=0;
57 while(pBuf[i2]=='\t'&&x<StartX){
58 i2++;
59 x++;
60 iFirstIndent_Specially++;
61 }
62
63 break;
64 }
65 }
66
67 //前の行のインデントをコピー(iIndentにタブ数を格納)
68 for(;i>0;i--){
69 if(
70 pBuf[i-1]=='\r'&&pBuf[i]=='\n'&&
71 (!(pBuf[i+1]=='\r'&&pBuf[i+2]=='\n'))
72 ){
73 i++;
74 break;
75 }
76 }
77
78 int iIndent;
79 for(iIndent=0;;iIndent++){
80 if(pBuf[i+iIndent]!='\t') break;
81 }
82
83 if(MdiInfo[WndNum].DocType==WNDTYPE_BASIC){
84 //インデント追加のコマンドかどうかを判断
85 if(IsIndentAdditionCommand_FromBuffer(pBuf+i+iIndent)) iIndent++;
86 }
87
88
89
90 //////////////////////////////
91 // 各行のTAB文字数を計算
92 //////////////////////////////
93
94 int *pTabs,LineCount=0;
95 pTabs=(int *)HeapAlloc(hHeap,0,(lstrlen(lpszTempReadBuffer)+1)*sizeof(int));
96
97 char *pTemp;
98 pTemp=(char *)HeapAlloc(hHeap,0,lstrlen(lpszTempReadBuffer)+1);
99
100 i=0;
101 BOOL bInterface=0;
102 while(1){
103 while(lpszTempReadBuffer[i]=='\t'||lpszTempReadBuffer[i]==' ') i++;
104
105 int PareNum,BracketNum;
106 PareNum=0,BracketNum=0;
107
108 pTabs[LineCount]=0;
109
110 int MultiLine=0;
111 for(i2=0;;i++,i2++){
112
113 if(lpszTempReadBuffer[i-1]=='('||lpszTempReadBuffer[i-1]=='[') PareNum++;
114 else if(lpszTempReadBuffer[i-1]==')'||lpszTempReadBuffer[i-1]==']') PareNum--;
115
116 if(lpszTempReadBuffer[i]=='\n'||lpszTempReadBuffer[i]=='\0'){
117 if((lpszTempReadBuffer[i-1]=='('||lpszTempReadBuffer[i-1]==',')&&lpszTempReadBuffer[i]){
118 //パラメータ改行
119
120 i++;
121 MultiLine++;
122
123 pTabs[LineCount+MultiLine]=PareNum;
124
125 while(lpszTempReadBuffer[i]=='\n'){
126 i++;
127 MultiLine++;
128
129 pTabs[LineCount+MultiLine]=PareNum;
130 }
131 continue;
132 }
133 if(lpszTempReadBuffer[i-1]=='_'&&lpszTempReadBuffer[i]){
134 //任意改行
135 i++;
136 i2--;
137
138 MultiLine++;
139
140 pTabs[LineCount+MultiLine]=1;
141 continue;
142 }
143
144 pTemp[i2]=0;
145 break;
146 }
147 pTemp[i2]=lpszTempReadBuffer[i];
148 }
149
150
151 //インデント減少コマンドかどうかを判定
152 int f;
153 if(f=IsIndentDecreaseCommand_FromBuffer(pTemp)){
154 if(iIndent>0) iIndent--;
155 if(f==2){
156 if(iIndent>0) iIndent--;
157 }
158
159 if(f==3) bInterface=0;
160 }
161
162 if(iFirstIndent_Specially!=-1){
163 if(iIndent-iFirstIndent_Specially<0) iFirstIndent_Specially=iIndent;
164
165 pTabs[LineCount]+=iIndent-iFirstIndent_Specially;
166
167 iFirstIndent_Specially=-1;
168 }
169 else pTabs[LineCount]+=iIndent;
170 LineCount++;
171
172 while(MultiLine){
173 pTabs[LineCount]+=iIndent;
174 LineCount++;
175
176 MultiLine--;
177 }
178
179
180
181 //インデント増加コマンドかどうかを判定
182 if((f=IsIndentAdditionCommand_FromBuffer(pTemp))&&bInterface==0){
183 iIndent++;
184 if(f==2) iIndent++;
185
186 if(f==3) bInterface=1;
187 }
188
189
190 if(lpszTempReadBuffer[i]=='\0') break;
191 i++;
192 }
193
194
195
196 //////////////////////////
197 // インデント整形
198 //////////////////////////
199
200 char *lpszNewBuffer,temp2[1024];
201 lpszNewBuffer=(char *)HeapAlloc(hHeap,0,lstrlen(lpszReadBuffer)+8192);
202 lpszNewBuffer[0]=0;
203
204 i=0;
205 bInterface=0;
206 LineCount=0;
207 while(1){
208 while(lpszReadBuffer[i]=='\t'||lpszReadBuffer[i]==' ') i++;
209
210 for(i2=0;;i++,i2++){
211
212 if(IS_RETURN(lpszReadBuffer,i)||lpszReadBuffer[i]=='\0'){
213 pTemp[i2]=0;
214 break;
215 }
216 pTemp[i2]=lpszReadBuffer[i];
217 }
218
219
220 memset(temp2,'\t',pTabs[LineCount]);
221 temp2[pTabs[LineCount]]=0;
222 LineCount++;
223
224 lstrcat(lpszNewBuffer,temp2);
225 lstrcat(lpszNewBuffer,pTemp);
226
227
228
229 if(lpszReadBuffer[i]=='\0') break;
230 i+=2;
231
232 lstrcat(lpszNewBuffer,"\r\n");
233 }
234
235 HeapDefaultFree(pTemp);
236 HeapDefaultFree(lpszTempReadBuffer);
237
238 HeapDefaultFree(pTabs);
239
240 return lpszNewBuffer;
241
242
243
244NonFormater:
245 //インデント整形は行わない
246 lpszNewBuffer=(char *)HeapAlloc(hHeap,0,lstrlen(lpszReadBuffer)+1);
247 lstrcpy(lpszNewBuffer,lpszReadBuffer);
248 return lpszNewBuffer;
249}
Note: See TracBrowser for help on using the repository browser.