TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kateautoindent.cpp 74KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  1. /* This file is part of the KDE libraries
  2. Copyright (C) 2003 Jesse Yurkovich <yurkjes@iit.edu>
  3. Copyright (C) 2004 >Anders Lund <anders@alweb.dk> (KateVarIndent class)
  4. Copyright (C) 2005 Dominik Haumann <dhdev@gmx.de> (basic support for config page)
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License version 2 as published by the Free Software Foundation.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include "kateautoindent.h"
  18. #include "kateautoindent.moc"
  19. #include "kateconfig.h"
  20. #include "katehighlight.h"
  21. #include "katefactory.h"
  22. #include "katejscript.h"
  23. #include "kateview.h"
  24. #include <tdelocale.h>
  25. #include <kdebug.h>
  26. #include <tdepopupmenu.h>
  27. #include <cctype>
  28. //BEGIN KateAutoIndent
  29. KateAutoIndent *KateAutoIndent::createIndenter (KateDocument *doc, uint mode)
  30. {
  31. if (mode == KateDocumentConfig::imNormal)
  32. return new KateNormalIndent (doc);
  33. else if (mode == KateDocumentConfig::imCStyle)
  34. return new KateCSmartIndent (doc);
  35. else if (mode == KateDocumentConfig::imPythonStyle)
  36. return new KatePythonIndent (doc);
  37. else if (mode == KateDocumentConfig::imXmlStyle)
  38. return new KateXmlIndent (doc);
  39. else if (mode == KateDocumentConfig::imCSAndS)
  40. return new KateCSAndSIndent (doc);
  41. else if ( mode == KateDocumentConfig::imVarIndent )
  42. return new KateVarIndent ( doc );
  43. // else if ( mode == KateDocumentConfig::imScriptIndent)
  44. // return new KateScriptIndent ( doc );
  45. return new KateAutoIndent (doc);
  46. }
  47. TQStringList KateAutoIndent::listModes ()
  48. {
  49. TQStringList l;
  50. l << modeDescription(KateDocumentConfig::imNone);
  51. l << modeDescription(KateDocumentConfig::imNormal);
  52. l << modeDescription(KateDocumentConfig::imCStyle);
  53. l << modeDescription(KateDocumentConfig::imPythonStyle);
  54. l << modeDescription(KateDocumentConfig::imXmlStyle);
  55. l << modeDescription(KateDocumentConfig::imCSAndS);
  56. l << modeDescription(KateDocumentConfig::imVarIndent);
  57. // l << modeDescription(KateDocumentConfig::imScriptIndent);
  58. return l;
  59. }
  60. TQString KateAutoIndent::modeName (uint mode)
  61. {
  62. if (mode == KateDocumentConfig::imNormal)
  63. return TQString ("normal");
  64. else if (mode == KateDocumentConfig::imCStyle)
  65. return TQString ("cstyle");
  66. else if (mode == KateDocumentConfig::imPythonStyle)
  67. return TQString ("python");
  68. else if (mode == KateDocumentConfig::imXmlStyle)
  69. return TQString ("xml");
  70. else if (mode == KateDocumentConfig::imCSAndS)
  71. return TQString ("csands");
  72. else if ( mode == KateDocumentConfig::imVarIndent )
  73. return TQString( "varindent" );
  74. // else if ( mode == KateDocumentConfig::imScriptIndent )
  75. // return TQString( "scriptindent" );
  76. return TQString ("none");
  77. }
  78. TQString KateAutoIndent::modeDescription (uint mode)
  79. {
  80. if (mode == KateDocumentConfig::imNormal)
  81. return i18n ("Normal");
  82. else if (mode == KateDocumentConfig::imCStyle)
  83. return i18n ("C Style");
  84. else if (mode == KateDocumentConfig::imPythonStyle)
  85. return i18n ("Python Style");
  86. else if (mode == KateDocumentConfig::imXmlStyle)
  87. return i18n ("XML Style");
  88. else if (mode == KateDocumentConfig::imCSAndS)
  89. return i18n ("S&S C Style");
  90. else if ( mode == KateDocumentConfig::imVarIndent )
  91. return i18n("Variable Based Indenter");
  92. // else if ( mode == KateDocumentConfig::imScriptIndent )
  93. // return i18n("JavaScript Indenter");
  94. return i18n ("None");
  95. }
  96. uint KateAutoIndent::modeNumber (const TQString &name)
  97. {
  98. if (modeName(KateDocumentConfig::imNormal) == name)
  99. return KateDocumentConfig::imNormal;
  100. else if (modeName(KateDocumentConfig::imCStyle) == name)
  101. return KateDocumentConfig::imCStyle;
  102. else if (modeName(KateDocumentConfig::imPythonStyle) == name)
  103. return KateDocumentConfig::imPythonStyle;
  104. else if (modeName(KateDocumentConfig::imXmlStyle) == name)
  105. return KateDocumentConfig::imXmlStyle;
  106. else if (modeName(KateDocumentConfig::imCSAndS) == name)
  107. return KateDocumentConfig::imCSAndS;
  108. else if ( modeName( KateDocumentConfig::imVarIndent ) == name )
  109. return KateDocumentConfig::imVarIndent;
  110. // else if ( modeName( KateDocumentConfig::imScriptIndent ) == name )
  111. // return KateDocumentConfig::imScriptIndent;
  112. return KateDocumentConfig::imNone;
  113. }
  114. bool KateAutoIndent::hasConfigPage (uint mode)
  115. {
  116. // if ( mode == KateDocumentConfig::imScriptIndent )
  117. // return true;
  118. return false;
  119. }
  120. IndenterConfigPage* KateAutoIndent::configPage(TQWidget *parent, uint mode)
  121. {
  122. // if ( mode == KateDocumentConfig::imScriptIndent )
  123. // return new ScriptIndentConfigPage(parent, "script_indent_config_page");
  124. return 0;
  125. }
  126. KateAutoIndent::KateAutoIndent (KateDocument *_doc)
  127. : TQObject(), doc(_doc)
  128. {
  129. }
  130. KateAutoIndent::~KateAutoIndent ()
  131. {
  132. }
  133. //END KateAutoIndent
  134. //BEGIN KateViewIndentAction
  135. KateViewIndentationAction::KateViewIndentationAction(KateDocument *_doc, const TQString& text, TQObject* parent, const char* name)
  136. : TDEActionMenu (text, parent, name), doc(_doc)
  137. {
  138. connect(popupMenu(),TQT_SIGNAL(aboutToShow()),this,TQT_SLOT(slotAboutToShow()));
  139. }
  140. void KateViewIndentationAction::slotAboutToShow()
  141. {
  142. TQStringList modes = KateAutoIndent::listModes ();
  143. popupMenu()->clear ();
  144. for (uint z=0; z<modes.size(); ++z)
  145. popupMenu()->insertItem ( '&' + KateAutoIndent::modeDescription(z).replace('&', "&&"), this, TQT_SLOT(setMode(int)), 0, z);
  146. popupMenu()->setItemChecked (doc->config()->indentationMode(), true);
  147. }
  148. void KateViewIndentationAction::setMode (int mode)
  149. {
  150. doc->config()->setIndentationMode((uint)mode);
  151. }
  152. //END KateViewIndentationAction
  153. //BEGIN KateNormalIndent
  154. KateNormalIndent::KateNormalIndent (KateDocument *_doc)
  155. : KateAutoIndent (_doc)
  156. {
  157. // if highlighting changes, update attributes
  158. connect(_doc, TQT_SIGNAL(hlChanged()), this, TQT_SLOT(updateConfig()));
  159. }
  160. KateNormalIndent::~KateNormalIndent ()
  161. {
  162. }
  163. void KateNormalIndent::updateConfig ()
  164. {
  165. KateDocumentConfig *config = doc->config();
  166. useSpaces = config->configFlags() & KateDocument::cfSpaceIndent || config->configFlags() & KateDocumentConfig::cfReplaceTabsDyn;
  167. mixedIndent = useSpaces && config->configFlags() & KateDocumentConfig::cfMixedIndent;
  168. keepProfile = config->configFlags() & KateDocument::cfKeepIndentProfile;
  169. tabWidth = config->tabWidth();
  170. indentWidth = useSpaces? config->indentationWidth() : tabWidth;
  171. commentAttrib = 255;
  172. doxyCommentAttrib = 255;
  173. regionAttrib = 255;
  174. symbolAttrib = 255;
  175. alertAttrib = 255;
  176. tagAttrib = 255;
  177. wordAttrib = 255;
  178. keywordAttrib = 255;
  179. normalAttrib = 255;
  180. extensionAttrib = 255;
  181. preprocessorAttrib = 255;
  182. stringAttrib = 255;
  183. charAttrib = 255;
  184. KateHlItemDataList items;
  185. doc->highlight()->getKateHlItemDataListCopy (0, items);
  186. for (uint i=0; i<items.count(); i++)
  187. {
  188. TQString name = items.at(i)->name;
  189. if (name.find("Comment") != -1 && commentAttrib == 255)
  190. {
  191. commentAttrib = i;
  192. }
  193. else if (name.find("Region Marker") != -1 && regionAttrib == 255)
  194. {
  195. regionAttrib = i;
  196. }
  197. else if (name.find("Symbol") != -1 && symbolAttrib == 255)
  198. {
  199. symbolAttrib = i;
  200. }
  201. else if (name.find("Alert") != -1)
  202. {
  203. alertAttrib = i;
  204. }
  205. else if (name.find("Comment") != -1 && commentAttrib != 255 && doxyCommentAttrib == 255)
  206. {
  207. doxyCommentAttrib = i;
  208. }
  209. else if (name.find("Tags") != -1 && tagAttrib == 255)
  210. {
  211. tagAttrib = i;
  212. }
  213. else if (name.find("Word") != -1 && wordAttrib == 255)
  214. {
  215. wordAttrib = i;
  216. }
  217. else if (name.find("Keyword") != -1 && keywordAttrib == 255)
  218. {
  219. keywordAttrib = i;
  220. }
  221. else if (name.find("Normal") != -1 && normalAttrib == 255)
  222. {
  223. normalAttrib = i;
  224. }
  225. else if (name.find("Extensions") != -1 && extensionAttrib == 255)
  226. {
  227. extensionAttrib = i;
  228. }
  229. else if (name.find("Preprocessor") != -1 && preprocessorAttrib == 255)
  230. {
  231. preprocessorAttrib = i;
  232. }
  233. else if (name.find("String") != -1 && stringAttrib == 255)
  234. {
  235. stringAttrib = i;
  236. }
  237. else if (name.find("Char") != -1 && charAttrib == 255)
  238. {
  239. charAttrib = i;
  240. }
  241. }
  242. }
  243. bool KateNormalIndent::isBalanced (KateDocCursor &begin, const KateDocCursor &end, TQChar open, TQChar close, uint &pos) const
  244. {
  245. int parenOpen = 0;
  246. bool atLeastOne = false;
  247. bool getNext = false;
  248. pos = doc->plainKateTextLine(begin.line())->firstChar();
  249. // Iterate one-by-one finding opening and closing chars
  250. // Assume that open and close are 'Symbol' characters
  251. while (begin < end)
  252. {
  253. TQChar c = begin.currentChar();
  254. if (begin.currentAttrib() == symbolAttrib)
  255. {
  256. if (c == open)
  257. {
  258. if (!atLeastOne)
  259. {
  260. atLeastOne = true;
  261. getNext = true;
  262. pos = measureIndent(begin) + 1;
  263. }
  264. parenOpen++;
  265. }
  266. else if (c == close)
  267. {
  268. parenOpen--;
  269. }
  270. }
  271. else if (getNext && !c.isSpace())
  272. {
  273. getNext = false;
  274. pos = measureIndent(begin);
  275. }
  276. if (atLeastOne && parenOpen <= 0)
  277. return true;
  278. if (!begin.moveForward(1))
  279. break;
  280. }
  281. return (atLeastOne) ? false : true;
  282. }
  283. bool KateNormalIndent::skipBlanks (KateDocCursor &cur, KateDocCursor &max, bool newline) const
  284. {
  285. int curLine = cur.line();
  286. if (newline)
  287. cur.moveForward(1);
  288. if (cur >= max)
  289. return false;
  290. do
  291. {
  292. uchar attrib = cur.currentAttrib();
  293. const TQString hlFile = doc->highlight()->hlKeyForAttrib( attrib );
  294. if (attrib != commentAttrib && attrib != regionAttrib && attrib != alertAttrib && attrib != preprocessorAttrib && !hlFile.endsWith("doxygen.xml"))
  295. {
  296. TQChar c = cur.currentChar();
  297. if (!c.isNull() && !c.isSpace())
  298. break;
  299. }
  300. if (!cur.moveForward(1))
  301. {
  302. // not able to move forward, so set cur to max
  303. cur = max;
  304. break;
  305. }
  306. // Make sure col is 0 if we spill into next line i.e. count the '\n' as a character
  307. if (curLine != cur.line())
  308. {
  309. if (!newline)
  310. break;
  311. curLine = cur.line();
  312. cur.setCol(0);
  313. }
  314. } while (cur < max);
  315. if (cur > max)
  316. cur = max;
  317. return true;
  318. }
  319. uint KateNormalIndent::measureIndent (KateDocCursor &cur) const
  320. {
  321. // We cannot short-cut by checking for useSpaces because there may be
  322. // tabs in the line despite this setting.
  323. return doc->plainKateTextLine(cur.line())->cursorX(cur.col(), tabWidth);
  324. }
  325. TQString KateNormalIndent::tabString(uint pos) const
  326. {
  327. TQString s;
  328. pos = kMin (pos, 80U); // sanity check for large values of pos
  329. if (!useSpaces || mixedIndent)
  330. {
  331. while (pos >= tabWidth)
  332. {
  333. s += '\t';
  334. pos -= tabWidth;
  335. }
  336. }
  337. while (pos > 0)
  338. {
  339. s += ' ';
  340. pos--;
  341. }
  342. return s;
  343. }
  344. void KateNormalIndent::processNewline (KateDocCursor &begin, bool /*needContinue*/)
  345. {
  346. int line = begin.line() - 1;
  347. int pos = begin.col();
  348. while ((line > 0) && (pos < 0)) // search a not empty text line
  349. pos = doc->plainKateTextLine(--line)->firstChar();
  350. if (pos > 0)
  351. {
  352. TQString filler = doc->text(line, 0, line, pos);
  353. doc->insertText(begin.line(), 0, filler);
  354. begin.setCol(filler.length());
  355. }
  356. else
  357. begin.setCol(0);
  358. }
  359. //END
  360. //BEGIN KateCSmartIndent
  361. KateCSmartIndent::KateCSmartIndent (KateDocument *doc)
  362. : KateNormalIndent (doc),
  363. allowSemi (false),
  364. processingBlock (false)
  365. {
  366. kdDebug(13030)<<"CREATING KATECSMART INTDETER"<<endl;
  367. }
  368. KateCSmartIndent::~KateCSmartIndent ()
  369. {
  370. }
  371. void KateCSmartIndent::processLine (KateDocCursor &line)
  372. {
  373. kdDebug(13030)<<"PROCESSING LINE "<<line.line()<<endl;
  374. KateTextLine::Ptr textLine = doc->plainKateTextLine(line.line());
  375. int firstChar = textLine->firstChar();
  376. // Empty line is worthless ... but only when doing more than 1 line
  377. if (firstChar == -1 && processingBlock)
  378. return;
  379. uint indent = 0;
  380. // TODO Here we do not check for beginning and ending comments ...
  381. TQChar first = textLine->getChar(firstChar);
  382. TQChar last = textLine->getChar(textLine->lastChar());
  383. if (first == '}')
  384. {
  385. indent = findOpeningBrace(line);
  386. }
  387. else if (first == ')')
  388. {
  389. indent = findOpeningParen(line);
  390. }
  391. else if (first == '{')
  392. {
  393. // If this is the first brace, we keep the indent at 0
  394. KateDocCursor temp(line.line(), firstChar, doc);
  395. if (!firstOpeningBrace(temp))
  396. indent = calcIndent(temp, false);
  397. }
  398. else if (first == ':')
  399. {
  400. // Initialization lists (handle c++ and c#)
  401. int pos = findOpeningBrace(line);
  402. if (pos == 0)
  403. indent = indentWidth;
  404. else
  405. indent = pos + (indentWidth * 2);
  406. }
  407. else if (last == ':')
  408. {
  409. if (textLine->stringAtPos (firstChar, "case") ||
  410. textLine->stringAtPos (firstChar, "default") ||
  411. textLine->stringAtPos (firstChar, "public") ||
  412. textLine->stringAtPos (firstChar, "private") ||
  413. textLine->stringAtPos (firstChar, "protected") ||
  414. textLine->stringAtPos (firstChar, "signals") ||
  415. textLine->stringAtPos (firstChar, "Q_SIGNALS") ||
  416. textLine->stringAtPos (firstChar, "Q_SLOTS") ||
  417. textLine->stringAtPos (firstChar, "slots"))
  418. {
  419. indent = findOpeningBrace(line) + indentWidth;
  420. }
  421. }
  422. else if (first == '*')
  423. {
  424. if (last == '/')
  425. {
  426. int lineEnd = textLine->lastChar();
  427. if (lineEnd > 0 && textLine->getChar(lineEnd - 1) == '*')
  428. {
  429. indent = findOpeningComment(line);
  430. if (textLine->attribute(firstChar) == doxyCommentAttrib)
  431. indent++;
  432. }
  433. else
  434. return;
  435. }
  436. else
  437. {
  438. KateDocCursor temp = line;
  439. if (textLine->attribute(firstChar) == doxyCommentAttrib)
  440. indent = calcIndent(temp, false) + 1;
  441. else
  442. indent = calcIndent(temp, true);
  443. }
  444. }
  445. else if (first == '#')
  446. {
  447. // c# regions
  448. if (textLine->stringAtPos (firstChar, "#region") ||
  449. textLine->stringAtPos (firstChar, "#endregion"))
  450. {
  451. KateDocCursor temp = line;
  452. indent = calcIndent(temp, true);
  453. }
  454. }
  455. else
  456. {
  457. // Everything else ...
  458. if (first == '/' && last != '/')
  459. return;
  460. KateDocCursor temp = line;
  461. indent = calcIndent(temp, true);
  462. if (indent == 0)
  463. {
  464. KateNormalIndent::processNewline(line, true);
  465. return;
  466. }
  467. }
  468. // Slightly faster if we don't indent what we don't have to
  469. if (indent != measureIndent(line) || first == '}' || first == '{' || first == '#')
  470. {
  471. doc->removeText(line.line(), 0, line.line(), firstChar);
  472. TQString filler = tabString(indent);
  473. if (indent > 0) doc->insertText(line.line(), 0, filler);
  474. if (!processingBlock) line.setCol(filler.length());
  475. }
  476. }
  477. void KateCSmartIndent::processSection (const KateDocCursor &begin, const KateDocCursor &end)
  478. {
  479. kdDebug(13030)<<"PROCESS SECTION"<<endl;
  480. KateDocCursor cur = begin;
  481. TQTime t;
  482. t.start();
  483. processingBlock = (end.line() - cur.line() > 0) ? true : false;
  484. while (cur.line() <= end.line())
  485. {
  486. processLine (cur);
  487. if (!cur.gotoNextLine())
  488. break;
  489. }
  490. processingBlock = false;
  491. kdDebug(13030) << "+++ total: " << t.elapsed() << endl;
  492. }
  493. bool KateCSmartIndent::handleDoxygen (KateDocCursor &begin)
  494. {
  495. // Factor out the rather involved Doxygen stuff here ...
  496. int line = begin.line();
  497. int first = -1;
  498. while ((line > 0) && (first < 0))
  499. first = doc->plainKateTextLine(--line)->firstChar();
  500. if (first >= 0)
  501. {
  502. KateTextLine::Ptr textLine = doc->plainKateTextLine(line);
  503. bool insideDoxygen = false;
  504. bool justAfterDoxygen = false;
  505. if (textLine->attribute(first) == doxyCommentAttrib || textLine->attribute(textLine->lastChar()) == doxyCommentAttrib)
  506. {
  507. const int last = textLine->lastChar();
  508. if (last <= 0 || !(justAfterDoxygen = textLine->stringAtPos(last-1, "*/")))
  509. insideDoxygen = true;
  510. if (justAfterDoxygen)
  511. justAfterDoxygen &= textLine->string().find("/**") < 0;
  512. while (textLine->attribute(first) != doxyCommentAttrib && first <= textLine->lastChar())
  513. first++;
  514. if (textLine->stringAtPos(first, "//"))
  515. return false;
  516. }
  517. // Align the *'s and then go ahead and insert one too ...
  518. if (insideDoxygen)
  519. {
  520. textLine = doc->plainKateTextLine(begin.line());
  521. first = textLine->firstChar();
  522. int indent = findOpeningComment(begin);
  523. TQString filler = tabString (indent);
  524. bool doxygenAutoInsert = doc->config()->configFlags() & KateDocumentConfig::cfDoxygenAutoTyping;
  525. if ( doxygenAutoInsert &&
  526. ((first < 0) || (!textLine->stringAtPos(first, "*/") && !textLine->stringAtPos(first, "*"))))
  527. {
  528. filler = filler + " * ";
  529. }
  530. doc->removeText (begin.line(), 0, begin.line(), first);
  531. doc->insertText (begin.line(), 0, filler);
  532. begin.setCol(filler.length());
  533. return true;
  534. }
  535. // Align position with beginning of doxygen comment. Otherwise the
  536. // indentation is one too much.
  537. else if (justAfterDoxygen)
  538. {
  539. textLine = doc->plainKateTextLine(begin.line());
  540. first = textLine->firstChar();
  541. int indent = findOpeningComment(begin);
  542. TQString filler = tabString (indent);
  543. doc->removeText (begin.line(), 0, begin.line(), first);
  544. doc->insertText (begin.line(), 0, filler);
  545. begin.setCol(filler.length());
  546. return true;
  547. }
  548. }
  549. return false;
  550. }
  551. void KateCSmartIndent::processNewline (KateDocCursor &begin, bool needContinue)
  552. {
  553. if (!handleDoxygen (begin))
  554. {
  555. KateTextLine::Ptr textLine = doc->plainKateTextLine(begin.line());
  556. bool inMiddle = textLine->firstChar() > -1;
  557. int indent = calcIndent (begin, needContinue);
  558. if (indent > 0 || inMiddle)
  559. {
  560. TQString filler = tabString (indent);
  561. doc->insertText (begin.line(), 0, filler);
  562. begin.setCol(filler.length());
  563. // Handles cases where user hits enter at the beginning or middle of text
  564. if (inMiddle)
  565. {
  566. processLine(begin);
  567. begin.setCol(textLine->firstChar());
  568. }
  569. }
  570. else
  571. {
  572. KateNormalIndent::processNewline (begin, needContinue);
  573. }
  574. if (begin.col() < 0)
  575. begin.setCol(0);
  576. }
  577. }
  578. /**
  579. * Returns true when the given attribute matches any "colon influence immune"
  580. * attribute
  581. * @param indenter indenter
  582. * @param attr1 attribute of previous char
  583. * @param attr2 attribute of char preceding previous char
  584. * @param prev1 previous character (0 if none)
  585. * @param prev2 character preceding previous character (0 if none)
  586. */
  587. static inline bool isColonImmune(const KateNormalIndent &indenter,
  588. uchar attr1, uchar attr2,
  589. TQChar prev1, TQChar prev2)
  590. {
  591. return attr1 == indenter.preprocessorAttrib
  592. // FIXME: no way to discriminate against multiline comment and single
  593. // line comment. Therefore, using prev? is futile.
  594. || attr1 == indenter.commentAttrib /*&& prev2 != '*' && prev1 != '/'*/
  595. || attr1 == indenter.doxyCommentAttrib
  596. || attr1 == indenter.stringAttrib && (attr2 != indenter.stringAttrib
  597. || (prev1 != '"' || prev2 == '\\' && attr2 == indenter.charAttrib))
  598. || prev1 == '\'' && attr1 != indenter.charAttrib;
  599. }
  600. /**
  601. * Returns true when the colon is allowed to reindent the current line
  602. * @param indenter current indenter
  603. * @param line current line
  604. * @param curCol column of most recently input character
  605. */
  606. static inline bool colonPermitsReindent(const KateNormalIndent &indenter,
  607. const KateTextLine::Ptr &line,
  608. int curCol
  609. )
  610. {
  611. const TQString txt = line->string(0,curCol);
  612. // do we have any significant preceding colon?
  613. for (int pos = 0; (pos = txt.find(':', pos)) >= 0; pos++) {
  614. if (line->attribute(pos) == indenter.symbolAttrib)
  615. // yes, it has already contributed to this line's indentation, don't
  616. // indent again
  617. return false;
  618. }
  619. // otherwise, check whether this colon is not within an influence
  620. // immune attribute range
  621. return !isColonImmune(indenter, line->attribute(curCol - 1),
  622. line->attribute(curCol - 2),
  623. txt[curCol - 1], txt[curCol - 2]);
  624. }
  625. void KateCSmartIndent::processChar(TQChar c)
  626. {
  627. // You may be curious about 'n' among the triggers:
  628. // It is used to discriminate C#'s #region/#endregion which are indented
  629. // against normal preprocessing statements which aren't indented.
  630. static const TQString triggers("}{)/:#n");
  631. static const TQString firstTriggers("}{)/:#");
  632. static const TQString lastTriggers(":n");
  633. if (triggers.find(c) < 0)
  634. return;
  635. KateView *view = doc->activeView();
  636. int curCol = view->cursorColumnReal() - 1;
  637. KateDocCursor begin(view->cursorLine(), 0, doc);
  638. KateTextLine::Ptr textLine = doc->plainKateTextLine(begin.line());
  639. const TQChar curChar = textLine->getChar(curCol);
  640. const int first = textLine->firstChar();
  641. const TQChar firstChar = textLine->getChar(first);
  642. #if 0 // nice try
  643. // Only indent on symbols or preprocessing directives -- never on
  644. // anything else
  645. kdDebug() << "curChar " << curChar << " curCol " << curCol << " textlen " << textLine->length() << " a " << textLine->attribute( curCol ) << " sym " << symbolAttrib << " pp " << preprocessorAttrib << endl;
  646. if (!(((curChar == '#' || curChar == 'n')
  647. && textLine->attribute( curCol ) == preprocessorAttrib)
  648. || textLine->attribute( curCol ) == symbolAttrib)
  649. )
  650. return;
  651. kdDebug() << "curChar " << curChar << endl;
  652. #endif
  653. if (c == 'n')
  654. {
  655. if (firstChar != '#' || textLine->string(curCol-5, 5) != TQString::fromLatin1("regio"))
  656. return;
  657. }
  658. if ( c == '/' )
  659. {
  660. // dominik: if line is "* /", change it to "*/"
  661. if ( textLine->attribute( begin.col() ) == doxyCommentAttrib )
  662. {
  663. // if the first char exists and is a '*', and the next non-space-char
  664. // is already the just typed '/', concatenate it to "*/".
  665. if ( first != -1
  666. && firstChar == '*'
  667. && textLine->nextNonSpaceChar( first+1 ) == view->cursorColumnReal()-1 )
  668. doc->removeText( view->cursorLine(), first+1, view->cursorLine(), view->cursorColumnReal()-1);
  669. }
  670. // ls: never have comments change the indentation.
  671. return;
  672. }
  673. // ls: only reindent line if the user actually expects it
  674. // I. e. take action on single braces on line or last colon, but inhibit
  675. // any reindentation if any of those characters appear amidst some section
  676. // of the line
  677. const TQChar lastChar = textLine->getChar(textLine->lastChar());
  678. int pos;
  679. if (((c == firstChar && firstTriggers.find(firstChar) >= 0)
  680. || (c == lastChar && lastTriggers.find(lastChar) >= 0))
  681. && (c != ':' || colonPermitsReindent(*this, textLine, curCol)))
  682. processLine(begin);
  683. }
  684. uint KateCSmartIndent::calcIndent(KateDocCursor &begin, bool needContinue)
  685. {
  686. KateTextLine::Ptr textLine;
  687. KateDocCursor cur = begin;
  688. uint anchorIndent = 0;
  689. int anchorPos = 0;
  690. int parenCount = 0; // Possibly in a multiline for stmt. Used to skip ';' ...
  691. bool found = false;
  692. bool isSpecial = false;
  693. bool potentialAnchorSeen = false;
  694. bool isArg = false; // ...arg,<newline>
  695. bool parenthesizedArg = false; // ...(arg,<newline>
  696. //kdDebug(13030) << "calcIndent begin line:" << begin.line() << " col:" << begin.col() << endl;
  697. // Find Indent Anchor Point
  698. while (cur.gotoPreviousLine())
  699. {
  700. isSpecial = found = false;
  701. textLine = doc->plainKateTextLine(cur.line());
  702. // Skip comments and handle cases like if (...) { stmt;
  703. int pos = textLine->lastChar();
  704. int openCount = 0;
  705. int otherAnchor = -1;
  706. do
  707. {
  708. if (textLine->attribute(pos) == symbolAttrib)
  709. {
  710. TQChar tc = textLine->getChar (pos);
  711. if ((tc == ';' || tc == ':' || tc == ',') && otherAnchor == -1 && parenCount <= 0) {
  712. otherAnchor = pos, potentialAnchorSeen = true;
  713. isArg = tc == ',';
  714. } else if (tc == ')')
  715. parenCount++;
  716. else if (tc == '(')
  717. parenCount--, parenthesizedArg = isArg, potentialAnchorSeen = true;
  718. else if (tc == '}')
  719. openCount--;
  720. else if (tc == '{')
  721. {
  722. openCount++, potentialAnchorSeen = true;
  723. if (openCount == 1)
  724. break;
  725. }
  726. }
  727. } while (--pos >= textLine->firstChar());
  728. if (openCount != 0 || otherAnchor != -1)
  729. {
  730. found = true;
  731. TQChar c;
  732. if (openCount > 0)
  733. c = '{';
  734. else if (openCount < 0)
  735. c = '}';
  736. else if (otherAnchor >= 0)
  737. c = textLine->getChar (otherAnchor);
  738. int specialIndent = 0;
  739. if (c == ':' && needContinue)
  740. {
  741. TQChar ch;
  742. specialIndent = textLine->firstChar();
  743. if (textLine->stringAtPos(specialIndent, "case"))
  744. ch = textLine->getChar(specialIndent + 4);
  745. else if (textLine->stringAtPos(specialIndent, "default"))
  746. ch = textLine->getChar(specialIndent + 7);
  747. else if (textLine->stringAtPos(specialIndent, "public"))
  748. ch = textLine->getChar(specialIndent + 6);
  749. else if (textLine->stringAtPos(specialIndent, "private"))
  750. ch = textLine->getChar(specialIndent + 7);
  751. else if (textLine->stringAtPos(specialIndent, "protected"))
  752. ch = textLine->getChar(specialIndent + 9);
  753. else if (textLine->stringAtPos(specialIndent, "signals"))
  754. ch = textLine->getChar(specialIndent + 7);
  755. else if (textLine->stringAtPos(specialIndent, "Q_SIGNALS"))
  756. ch = textLine->getChar(specialIndent + 9);
  757. else if (textLine->stringAtPos(specialIndent, "slots"))
  758. ch = textLine->getChar(specialIndent + 5);
  759. else if (textLine->stringAtPos(specialIndent, "Q_SLOTS"))
  760. ch = textLine->getChar(specialIndent + 7);
  761. if (ch.isNull() || (!ch.isSpace() && ch != '(' && ch != ':'))
  762. continue;
  763. KateDocCursor lineBegin = cur;
  764. lineBegin.setCol(specialIndent);
  765. specialIndent = measureIndent(lineBegin);
  766. isSpecial = true;
  767. }
  768. // Move forward past blank lines
  769. KateDocCursor skip = cur;
  770. skip.setCol(textLine->lastChar());
  771. bool result = skipBlanks(skip, begin, true);
  772. anchorPos = skip.col();
  773. anchorIndent = measureIndent(skip);
  774. //kdDebug(13030) << "calcIndent anchorPos:" << anchorPos << " anchorIndent:" << anchorIndent << " at line:" << skip.line() << endl;
  775. // Accept if it's before requested position or if it was special
  776. if (result && skip < begin)
  777. {
  778. cur = skip;
  779. break;
  780. }
  781. else if (isSpecial)
  782. {
  783. anchorIndent = specialIndent;
  784. break;
  785. }
  786. // Are these on a line by themselves? (i.e. both last and first char)
  787. if ((c == '{' || c == '}') && textLine->getChar(textLine->firstChar()) == c)
  788. {
  789. cur.setCol(anchorPos = textLine->firstChar());
  790. anchorIndent = measureIndent (cur);
  791. break;
  792. }
  793. }
  794. }
  795. // treat beginning of document as anchor position
  796. if (cur.line() == 0 && cur.col() == 0 && potentialAnchorSeen)
  797. found = true;
  798. if (!found)
  799. return 0;
  800. uint continueIndent = (needContinue) ? calcContinue (cur, begin) : 0;
  801. //kdDebug(13030) << "calcIndent continueIndent:" << continueIndent << endl;
  802. // Move forward from anchor and determine last known reference character
  803. // Braces take precedance over others ...
  804. textLine = doc->plainKateTextLine(cur.line());
  805. TQChar lastChar = textLine->getChar (anchorPos);
  806. int lastLine = cur.line();
  807. if (lastChar == '#' || lastChar == '[')
  808. {
  809. // Never continue if # or [ is encountered at this point here
  810. // A fail-safe really... most likely an #include, #region, or a c# attribute
  811. continueIndent = 0;
  812. }
  813. int openCount = 0;
  814. while (cur.validPosition() && cur < begin)
  815. {
  816. if (!skipBlanks(cur, begin, true))
  817. return isArg && !parenthesizedArg ? begin.col() : 0;
  818. TQChar tc = cur.currentChar();
  819. //kdDebug(13030) << " cur.line:" << cur.line() << " cur.col:" << cur.col() << " currentChar '" << tc << "' " << textLine->attribute(cur.col()) << endl;
  820. if (cur == begin || tc.isNull())
  821. break;
  822. if (!tc.isSpace() && cur < begin)
  823. {
  824. uchar attrib = cur.currentAttrib();
  825. if (tc == '{' && attrib == symbolAttrib)
  826. openCount++;
  827. else if (tc == '}' && attrib == symbolAttrib)
  828. openCount--;
  829. lastChar = tc;
  830. lastLine = cur.line();
  831. }
  832. }
  833. if (openCount > 0) // Open braces override
  834. lastChar = '{';
  835. uint indent = 0;
  836. //kdDebug(13030) << "calcIndent lastChar '" << lastChar << "'" << endl;
  837. if (lastChar == '{' || (lastChar == ':' && isSpecial && needContinue))
  838. {
  839. indent = anchorIndent + indentWidth;
  840. }
  841. else if (lastChar == '}')
  842. {
  843. indent = anchorIndent;
  844. }
  845. else if (lastChar == ';')
  846. {
  847. indent = anchorIndent + ((allowSemi && needContinue) ? continueIndent : 0);
  848. }
  849. else if (lastChar == ',' || lastChar == '(')
  850. {
  851. textLine = doc->plainKateTextLine(lastLine);
  852. KateDocCursor start(lastLine, textLine->firstChar(), doc);
  853. KateDocCursor finish(lastLine, textLine->lastChar() + 1, doc);
  854. uint pos = 0;
  855. if (isBalanced(start, finish, TQChar('('), TQChar(')'), pos) && false)
  856. indent = anchorIndent;
  857. else
  858. {
  859. // TODO: Config option. If we're below 48, go ahead and line them up
  860. indent = ((pos < 48) ? pos : anchorIndent + (indentWidth * 2));
  861. }
  862. }
  863. else if (!lastChar.isNull())
  864. {
  865. if (anchorIndent != 0)
  866. indent = anchorIndent + continueIndent;
  867. else
  868. indent = continueIndent;
  869. }
  870. return indent;
  871. }
  872. uint KateCSmartIndent::calcContinue(KateDocCursor &start, KateDocCursor &end)
  873. {
  874. KateDocCursor cur = start;
  875. bool needsBalanced = true;
  876. bool isFor = false;
  877. allowSemi = false;
  878. KateTextLine::Ptr textLine = doc->plainKateTextLine(cur.line());
  879. // Handle cases such as } while (s ... by skipping the leading symbol
  880. if (textLine->attribute(cur.col()) == symbolAttrib)
  881. {
  882. cur.moveForward(1);
  883. skipBlanks(cur, end, false);
  884. }
  885. if (textLine->getChar(cur.col()) == '}')
  886. {
  887. skipBlanks(cur, end, true);
  888. if (cur.line() != start.line())
  889. textLine = doc->plainKateTextLine(cur.line());
  890. if (textLine->stringAtPos(cur.col(), "else"))
  891. cur.setCol(cur.col() + 4);
  892. else
  893. return indentWidth * 2;
  894. needsBalanced = false;
  895. }
  896. else if (textLine->stringAtPos(cur.col(), "else"))
  897. {
  898. cur.setCol(cur.col() + 4);
  899. needsBalanced = false;
  900. int next = textLine->nextNonSpaceChar(cur.col());
  901. if (next >= 0 && textLine->stringAtPos(next, "if"))
  902. {
  903. cur.setCol(next + 2);
  904. needsBalanced = true;
  905. }
  906. }
  907. else if (textLine->stringAtPos(cur.col(), "if"))
  908. {
  909. cur.setCol(cur.col() + 2);
  910. }
  911. else if (textLine->stringAtPos(cur.col(), "do"))
  912. {
  913. cur.setCol(cur.col() + 2);
  914. needsBalanced = false;
  915. }
  916. else if (textLine->stringAtPos(cur.col(), "for"))
  917. {
  918. cur.setCol(cur.col() + 3);
  919. isFor = true;
  920. }
  921. else if (textLine->stringAtPos(cur.col(), "while"))
  922. {
  923. cur.setCol(cur.col() + 5);
  924. }
  925. else if (textLine->stringAtPos(cur.col(), "switch"))
  926. {
  927. cur.setCol(cur.col() + 6);
  928. }
  929. else if (textLine->stringAtPos(cur.col(), "using"))
  930. {
  931. cur.setCol(cur.col() + 5);
  932. }
  933. else
  934. {
  935. return indentWidth * 2;
  936. }
  937. uint openPos = 0;
  938. if (needsBalanced && !isBalanced (cur, end, TQChar('('), TQChar(')'), openPos))
  939. {
  940. allowSemi = isFor;
  941. if (openPos > 0)
  942. return (openPos - textLine->firstChar());
  943. else
  944. return indentWidth * 2;
  945. }
  946. // Check if this statement ends a line now
  947. skipBlanks(cur, end, false);
  948. if (cur == end)
  949. return indentWidth;
  950. if (skipBlanks(cur, end, true))
  951. {
  952. if (cur == end)
  953. return indentWidth;
  954. else
  955. return indentWidth + calcContinue(cur, end);
  956. }
  957. return 0;
  958. }
  959. uint KateCSmartIndent::findOpeningBrace(KateDocCursor &start)
  960. {
  961. KateDocCursor cur = start;
  962. int count = 1;
  963. // Move backwards 1 by 1 and find the opening brace
  964. // Return the indent of that line
  965. while (cur.moveBackward(1))
  966. {
  967. if (cur.currentAttrib() == symbolAttrib)
  968. {
  969. TQChar ch = cur.currentChar();
  970. if (ch == '{')
  971. count--;
  972. else if (ch == '}')
  973. count++;
  974. if (count == 0)
  975. {
  976. KateDocCursor temp(cur.line(), doc->plainKateTextLine(cur.line())->firstChar(), doc);
  977. return measureIndent(temp);
  978. }
  979. }
  980. }
  981. return 0;
  982. }
  983. bool KateCSmartIndent::firstOpeningBrace(KateDocCursor &start)
  984. {
  985. KateDocCursor cur = start;
  986. // Are we the first opening brace at this level?
  987. while(cur.moveBackward(1))
  988. {
  989. if (cur.currentAttrib() == symbolAttrib)
  990. {
  991. TQChar ch = cur.currentChar();
  992. if (ch == '{')
  993. return false;
  994. else if (ch == '}' && cur.col() == 0)
  995. break;
  996. }
  997. }
  998. return true;
  999. }
  1000. uint KateCSmartIndent::findOpeningParen(KateDocCursor &start)
  1001. {
  1002. KateDocCursor cur = start;
  1003. int count = 1;
  1004. // Move backwards 1 by 1 and find the opening (
  1005. // Return the indent of that line
  1006. while (cur.moveBackward(1))
  1007. {
  1008. if (cur.currentAttrib() == symbolAttrib)
  1009. {
  1010. TQChar ch = cur.currentChar();
  1011. if (ch == '(')
  1012. count--;
  1013. else if (ch == ')')
  1014. count++;
  1015. if (count == 0)
  1016. return measureIndent(cur);
  1017. }
  1018. }
  1019. return 0;
  1020. }
  1021. uint KateCSmartIndent::findOpeningComment(KateDocCursor &start)
  1022. {
  1023. KateDocCursor cur = start;
  1024. // Find the line with the opening /* and return the proper indent
  1025. do
  1026. {
  1027. KateTextLine::Ptr textLine = doc->plainKateTextLine(cur.line());
  1028. int pos = textLine->string().find("/*", false);
  1029. if (pos >= 0)
  1030. {
  1031. KateDocCursor temp(cur.line(), pos, doc);
  1032. return measureIndent(temp);
  1033. }
  1034. } while (cur.gotoPreviousLine());
  1035. return 0;
  1036. }
  1037. //END
  1038. //BEGIN KatePythonIndent
  1039. TQRegExp KatePythonIndent::endWithColon = TQRegExp( "^[^#]*:\\s*(#.*)?$" );
  1040. TQRegExp KatePythonIndent::stopStmt = TQRegExp( "^\\s*(break|continue|raise|return|pass)\\b.*" );
  1041. TQRegExp KatePythonIndent::blockBegin = TQRegExp( "^\\s*(class|def|if|elif|else|for|while|try)\\b.*" );
  1042. KatePythonIndent::KatePythonIndent (KateDocument *doc)
  1043. : KateNormalIndent (doc)
  1044. {
  1045. }
  1046. KatePythonIndent::~KatePythonIndent ()
  1047. {
  1048. }
  1049. void KatePythonIndent::processNewline (KateDocCursor &begin, bool /*newline*/)
  1050. {
  1051. int prevLine = begin.line() - 1;
  1052. int prevPos = begin.col();
  1053. while ((prevLine > 0) && (prevPos < 0)) // search a not empty text line
  1054. prevPos = doc->plainKateTextLine(--prevLine)->firstChar();
  1055. int prevBlock = prevLine;
  1056. int prevBlockPos = prevPos;
  1057. int extraIndent = calcExtra (prevBlock, prevBlockPos, begin);
  1058. int indent = doc->plainKateTextLine(prevBlock)->cursorX(prevBlockPos, tabWidth);
  1059. if (extraIndent == 0)
  1060. {
  1061. if (!stopStmt.exactMatch(doc->plainKateTextLine(prevLine)->string()))
  1062. {
  1063. if (endWithColon.exactMatch(doc->plainKateTextLine(prevLine)->string()))
  1064. indent += indentWidth;
  1065. else
  1066. indent = doc->plainKateTextLine(prevLine)->cursorX(prevPos, tabWidth);
  1067. }
  1068. }
  1069. else
  1070. indent += extraIndent;
  1071. if (indent > 0)
  1072. {
  1073. TQString filler = tabString (indent);
  1074. doc->insertText (begin.line(), 0, filler);
  1075. begin.setCol(filler.length());
  1076. }
  1077. else
  1078. begin.setCol(0);
  1079. }
  1080. int KatePythonIndent::calcExtra (int &prevBlock, int &pos, KateDocCursor &end)
  1081. {
  1082. int nestLevel = 0;
  1083. bool levelFound = false;
  1084. while ((prevBlock > 0))
  1085. {
  1086. if (blockBegin.exactMatch(doc->plainKateTextLine(prevBlock)->string()))
  1087. {
  1088. if ((!levelFound && nestLevel == 0) || (levelFound && nestLevel - 1 <= 0))
  1089. {
  1090. pos = doc->plainKateTextLine(prevBlock)->firstChar();
  1091. break;
  1092. }
  1093. nestLevel --;
  1094. }
  1095. else if (stopStmt.exactMatch(doc->plainKateTextLine(prevBlock)->string()))
  1096. {
  1097. nestLevel ++;
  1098. levelFound = true;
  1099. }
  1100. --prevBlock;
  1101. }
  1102. KateDocCursor cur (prevBlock, pos, doc);
  1103. TQChar c;
  1104. int extraIndent = 0;
  1105. while (cur.line() < end.line())
  1106. {
  1107. c = cur.currentChar();
  1108. if (c == '(')
  1109. extraIndent += indentWidth;
  1110. else if (c == ')')
  1111. extraIndent -= indentWidth;
  1112. else if (c == ':')
  1113. break;
  1114. else if (c == '\'' || c == '"' )
  1115. traverseString( c, cur, end );
  1116. if (c.isNull() || c == '#')
  1117. cur.gotoNextLine();
  1118. else
  1119. cur.moveForward(1);
  1120. }
  1121. return extraIndent;
  1122. }
  1123. void KatePythonIndent::traverseString( const TQChar &stringChar, KateDocCursor &cur, KateDocCursor &end )
  1124. {
  1125. TQChar c;
  1126. bool escape = false;
  1127. cur.moveForward(1);
  1128. c = cur.currentChar();
  1129. while ( ( c != stringChar || escape ) && cur.line() < end.line() )
  1130. {
  1131. if ( escape )
  1132. escape = false;
  1133. else if ( c == '\\' )
  1134. escape = !escape;
  1135. cur.moveForward(1);
  1136. c = cur.currentChar();
  1137. }
  1138. }
  1139. //END
  1140. //BEGIN KateXmlIndent
  1141. /* Explanation
  1142. The XML indenter simply inherits the indentation of the previous line,
  1143. with the first line starting at 0 (of course!). For each element that
  1144. is opened on the previous line, the indentation is increased by one
  1145. level; for each element that is closed, it is decreased by one.
  1146. We also have a special case of opening an element on one line and then
  1147. entering attributes on the following lines, in which case we would like
  1148. to see the following layout:
  1149. <elem attr="..."
  1150. blah="..." />
  1151. <x><a href="..."
  1152. title="..." />
  1153. </x>
  1154. This is accomplished by checking for lines that contain an unclosed open
  1155. tag.
  1156. */
  1157. const TQRegExp KateXmlIndent::startsWithCloseTag("^[ \t]*</");
  1158. const TQRegExp KateXmlIndent::unclosedDoctype("<!DOCTYPE[^>]*$");
  1159. KateXmlIndent::KateXmlIndent (KateDocument *doc)
  1160. : KateNormalIndent (doc)
  1161. {
  1162. }
  1163. KateXmlIndent::~KateXmlIndent ()
  1164. {
  1165. }
  1166. void KateXmlIndent::processNewline (KateDocCursor &begin, bool /*newline*/)
  1167. {
  1168. begin.setCol(processLine(begin.line()));
  1169. }
  1170. void KateXmlIndent::processChar (TQChar c)
  1171. {
  1172. if(c != '/') return;
  1173. // only alter lines that start with a close element
  1174. KateView *view = doc->activeView();
  1175. TQString text = doc->plainKateTextLine(view->cursorLine())->string();
  1176. if(text.find(startsWithCloseTag) == -1) return;
  1177. // process it
  1178. processLine(view->cursorLine());
  1179. }
  1180. void KateXmlIndent::processLine (KateDocCursor &line)
  1181. {
  1182. processLine (line.line());
  1183. }
  1184. void KateXmlIndent::processSection (const KateDocCursor &start, const KateDocCursor &end)
  1185. {
  1186. KateDocCursor cur (start);
  1187. int endLine = end.line();
  1188. do {
  1189. processLine(cur.line());
  1190. if(!cur.gotoNextLine()) break;
  1191. } while(cur.line() < endLine);
  1192. }
  1193. void KateXmlIndent::getLineInfo (uint line, uint &prevIndent, int &numTags,
  1194. uint &attrCol, bool &unclosedTag)
  1195. {
  1196. prevIndent = 0;
  1197. int firstChar;
  1198. KateTextLine::Ptr prevLine = 0;
  1199. // get the indentation of the first non-empty line
  1200. while(true) {
  1201. prevLine = doc->plainKateTextLine(line);
  1202. if( (firstChar = prevLine->firstChar()) < 0) {
  1203. if(!line--) return;
  1204. continue;
  1205. }
  1206. break;
  1207. }
  1208. prevIndent = prevLine->cursorX(prevLine->firstChar(), tabWidth);
  1209. TQString text = prevLine->string();
  1210. // special case:
  1211. // <a>
  1212. // </a> <!-- indentation *already* decreased -->
  1213. // requires that we discount the </a> from the number of closed tags
  1214. if(text.find(startsWithCloseTag) != -1) ++numTags;
  1215. // count the number of open and close tags
  1216. int lastCh = 0;
  1217. uint pos, len = text.length();
  1218. bool seenOpen = false;
  1219. for(pos = 0; pos < len; ++pos) {
  1220. int ch = text.at(pos).unicode();
  1221. switch(ch) {
  1222. case '<':
  1223. seenOpen = true;
  1224. unclosedTag = true;
  1225. attrCol = pos;
  1226. ++numTags;
  1227. break;
  1228. // don't indent because of DOCTYPE, comment, CDATA, etc.
  1229. case '!':
  1230. if(lastCh == '<') --numTags;
  1231. break;
  1232. // don't indent because of xml decl or PI
  1233. case '?':
  1234. if(lastCh == '<') --numTags;
  1235. break;
  1236. case '>':
  1237. if(!seenOpen) {
  1238. // we are on a line like the second one here:
  1239. // <element attr="val"
  1240. // other="val">
  1241. // so we need to set prevIndent to the indent of the first line
  1242. //
  1243. // however, we need to special case "<!DOCTYPE" because
  1244. // it's not an open tag
  1245. prevIndent = 0;
  1246. for(uint backLine = line; backLine; ) {
  1247. // find first line with an open tag
  1248. KateTextLine::Ptr x = doc->plainKateTextLine(--backLine);
  1249. if(x->string().find('<') == -1) continue;
  1250. // recalculate the indent
  1251. if(x->string().find(unclosedDoctype) != -1) --numTags;
  1252. getLineInfo(backLine, prevIndent, numTags, attrCol, unclosedTag);
  1253. break;
  1254. }
  1255. }
  1256. if(lastCh == '/') --numTags;
  1257. unclosedTag = false;
  1258. break;
  1259. case '/':
  1260. if(lastCh == '<') numTags -= 2; // correct for '<', above
  1261. break;
  1262. }
  1263. lastCh = ch;
  1264. }
  1265. if(unclosedTag) {
  1266. // find the start of the next attribute, so we can align with it
  1267. do {
  1268. lastCh = text.at(++attrCol).unicode();
  1269. }while(lastCh && lastCh != ' ' && lastCh != '\t');
  1270. while(lastCh == ' ' || lastCh == '\t') {
  1271. lastCh = text.at(++attrCol).unicode();
  1272. }
  1273. attrCol = prevLine->cursorX(attrCol, tabWidth);
  1274. }
  1275. }
  1276. uint KateXmlIndent::processLine (uint line)
  1277. {
  1278. KateTextLine::Ptr kateLine = doc->plainKateTextLine(line);
  1279. if(!kateLine) return 0; // sanity check
  1280. // get details from previous line
  1281. uint prevIndent = 0, attrCol = 0;
  1282. int numTags = 0;
  1283. bool unclosedTag = false; // for aligning attributes
  1284. if(line) {
  1285. getLineInfo(line - 1, prevIndent, numTags, attrCol, unclosedTag);
  1286. }
  1287. // compute new indent
  1288. int indent = 0;
  1289. if(unclosedTag) indent = attrCol;
  1290. else indent = prevIndent + numTags * indentWidth;
  1291. if(indent < 0) indent = 0;
  1292. // unindent lines that start with a close tag
  1293. if(kateLine->string().find(startsWithCloseTag) != -1) {
  1294. indent -= indentWidth;
  1295. }
  1296. if(indent < 0) indent = 0;
  1297. // apply new indent
  1298. doc->removeText(line, 0, line, kateLine->firstChar());
  1299. TQString filler = tabString(indent);
  1300. doc->insertText(line, 0, filler);
  1301. return filler.length();
  1302. }
  1303. //END
  1304. //BEGIN KateCSAndSIndent
  1305. KateCSAndSIndent::KateCSAndSIndent (KateDocument *doc)
  1306. : KateNormalIndent (doc)
  1307. {
  1308. }
  1309. void KateCSAndSIndent::updateIndentString()
  1310. {
  1311. if( useSpaces )
  1312. indentString.fill( ' ', indentWidth );
  1313. else
  1314. indentString = '\t';
  1315. }
  1316. KateCSAndSIndent::~KateCSAndSIndent ()
  1317. {
  1318. }
  1319. void KateCSAndSIndent::processLine (KateDocCursor &line)
  1320. {
  1321. KateTextLine::Ptr textLine = doc->plainKateTextLine(line.line());
  1322. if (!textLine)
  1323. return;
  1324. updateIndentString();
  1325. const int oldCol = line.col();
  1326. TQString whitespace = calcIndent(line);
  1327. // strip off existing whitespace
  1328. int oldIndent = textLine->firstChar();
  1329. if ( oldIndent < 0 )
  1330. oldIndent = doc->lineLength( line.line() );
  1331. if( oldIndent > 0 )
  1332. doc->removeText(line.line(), 0, line.line(), oldIndent);
  1333. // add correct amount
  1334. doc->insertText(line.line(), 0, whitespace);
  1335. // try to preserve the cursor position in the line
  1336. if ( int(oldCol + whitespace.length()) >= oldIndent )
  1337. line.setCol( oldCol + whitespace.length() - oldIndent );
  1338. else
  1339. line.setCol( 0 );
  1340. }
  1341. void KateCSAndSIndent::processSection (const KateDocCursor &begin, const KateDocCursor &end)
  1342. {
  1343. TQTime t; t.start();
  1344. for( KateDocCursor cur = begin; cur.line() <= end.line(); )
  1345. {
  1346. processLine (cur);
  1347. if (!cur.gotoNextLine())
  1348. break;
  1349. }
  1350. kdDebug(13030) << "+++ total: " << t.elapsed() << endl;
  1351. }
  1352. /**
  1353. * Returns the first @p chars characters of @p line, converted to whitespace.
  1354. * If @p convert is set to false, characters at and after the first non-whitespace
  1355. * character are removed, not converted.
  1356. */
  1357. static TQString initialWhitespace(const KateTextLine::Ptr &line, int chars, bool convert = true)
  1358. {
  1359. TQString text = line->string(0, chars);
  1360. if( (int)text.length() < chars )
  1361. {
  1362. TQString filler; filler.fill(' ',chars - text.length());
  1363. text += filler;
  1364. }
  1365. for( uint n = 0; n < text.length(); ++n )
  1366. {
  1367. if( text[n] != '\t' && text[n] != ' ' )
  1368. {
  1369. if( !convert )
  1370. return text.left( n );
  1371. text[n] = ' ';
  1372. }
  1373. }
  1374. return text;
  1375. }
  1376. TQString KateCSAndSIndent::findOpeningCommentIndentation(const KateDocCursor &start)
  1377. {
  1378. KateDocCursor cur = start;
  1379. // Find the line with the opening /* and return the indentation of it
  1380. do
  1381. {
  1382. KateTextLine::Ptr textLine = doc->plainKateTextLine(cur.line());
  1383. int pos = textLine->string().findRev("/*");
  1384. // FIXME: /* inside /* is possible. This screws up in that case...
  1385. if (pos >= 0)
  1386. return initialWhitespace(textLine, pos);
  1387. } while (cur.gotoPreviousLine());
  1388. // should never happen.
  1389. kdWarning( 13030 ) << " in a comment, but can't find the start of it" << endl;
  1390. return TQString::null;
  1391. }
  1392. bool KateCSAndSIndent::handleDoxygen (KateDocCursor &begin)
  1393. {
  1394. // Look backwards for a nonempty line
  1395. int line = begin.line();
  1396. int first = -1;
  1397. while ((line > 0) && (first < 0))
  1398. first = doc->plainKateTextLine(--line)->firstChar();
  1399. // no earlier nonempty line
  1400. if (first < 0)
  1401. return false;
  1402. KateTextLine::Ptr textLine = doc->plainKateTextLine(line);
  1403. // if the line doesn't end with a doxygen comment (that's not closed)
  1404. // and doesn't start with a doxygen comment (that's not closed), we don't care.
  1405. // note that we do need to check the start of the line, or lines ending with, say, @brief aren't
  1406. // recognised.
  1407. if ( !(textLine->attribute(textLine->lastChar()) == doxyCommentAttrib && !textLine->endingWith("*/")) &&
  1408. !(textLine->attribute(textLine->firstChar()) == doxyCommentAttrib && !textLine->string().contains("*/")) )
  1409. return false;
  1410. // our line is inside a doxygen comment. align the *'s and then maybe insert one too ...
  1411. textLine = doc->plainKateTextLine(begin.line());
  1412. first = textLine->firstChar();
  1413. TQString indent = findOpeningCommentIndentation(begin);
  1414. bool doxygenAutoInsert = doc->config()->configFlags() & KateDocumentConfig::cfDoxygenAutoTyping;
  1415. // starts with *: indent one space more to line up *s
  1416. if ( first >= 0 && textLine->stringAtPos(first, "*") )
  1417. indent = indent + " ";
  1418. // does not start with *: insert one if user wants that
  1419. else if ( doxygenAutoInsert )
  1420. indent = indent + " * ";
  1421. // user doesn't want * inserted automatically: put in spaces?
  1422. //else
  1423. // indent = indent + " ";
  1424. doc->removeText (begin.line(), 0, begin.line(), first);
  1425. doc->insertText (begin.line(), 0, indent);
  1426. begin.setCol(indent.length());
  1427. return true;
  1428. }
  1429. /**
  1430. * @brief User pressed enter. Line has been split; begin is on the new line.
  1431. * @param begin Three unrelated variables: the new line number, where the first
  1432. * non-whitespace char was on the previous line, and the document.
  1433. * @param needContinue Something to do with indenting the current line; always true.
  1434. */
  1435. void KateCSAndSIndent::processNewline (KateDocCursor &begin, bool /*needContinue*/)
  1436. {
  1437. // in a comment, add a * doxygen-style.
  1438. if( handleDoxygen(begin) )
  1439. return;
  1440. // TODO: if the user presses enter in the middle of a label, maybe the first half of the
  1441. // label should be indented?
  1442. // where the cursor actually is...
  1443. int cursorPos = doc->plainKateTextLine( begin.line() )->firstChar();
  1444. if ( cursorPos < 0 )
  1445. cursorPos = doc->lineLength( begin.line() );
  1446. begin.setCol( cursorPos );
  1447. processLine( begin );
  1448. }
  1449. /**
  1450. * Does the line @p line start with a label?
  1451. * @note May also return @c true if the line starts in a continuation.
  1452. */
  1453. bool KateCSAndSIndent::startsWithLabel( int line )
  1454. {
  1455. // Get the current line.
  1456. KateTextLine::Ptr indentLine = doc->plainKateTextLine(line);
  1457. const int indentFirst = indentLine->firstChar();
  1458. // Not entirely sure what this check does.
  1459. int attrib = indentLine->attribute(indentFirst);
  1460. if (attrib != 0 && attrib != keywordAttrib && attrib != normalAttrib && attrib != extensionAttrib)
  1461. return false;
  1462. // Get the line text.
  1463. const TQString lineContents = indentLine->string();
  1464. const int indentLast = indentLine->lastChar();
  1465. bool whitespaceFound = false;
  1466. for ( int n = indentFirst; n <= indentLast; ++n )
  1467. {
  1468. // Get the character as latin1. Can't use TQChar::isLetterOrNumber()
  1469. // as that includes non 0-9 numbers.
  1470. char c = lineContents[n].latin1();
  1471. if ( c == ':' )
  1472. {
  1473. // See if the next character is ':' - if so, skip to the character after it.
  1474. if ( n < lineContents.length() - 1 )
  1475. {
  1476. if ( lineContents[n+1].latin1() == ':' )
  1477. {
  1478. n += 2;
  1479. continue;
  1480. }
  1481. }
  1482. // Right this is the relevent ':'.
  1483. if ( n == indentFirst)
  1484. {
  1485. // Just a line with a : on it.
  1486. return false;
  1487. }
  1488. // It is a label of some kind!
  1489. return true;
  1490. }
  1491. if (isspace(c))
  1492. {
  1493. if (!whitespaceFound)
  1494. {
  1495. if (lineContents.mid(indentFirst, n - indentFirst) == "case")
  1496. return true;
  1497. else if (lineContents.mid(indentFirst, n - indentFirst) == "class")
  1498. return false;
  1499. whitespaceFound = true;
  1500. }
  1501. }
  1502. // All other characters don't indent.
  1503. else if ( !isalnum(c) && c != '_' )
  1504. {
  1505. return false;
  1506. }
  1507. }
  1508. return false;
  1509. }
  1510. template<class T> T min(T a, T b) { return (a < b) ? a : b; }
  1511. int KateCSAndSIndent::lastNonCommentChar( const KateDocCursor &line )
  1512. {
  1513. KateTextLine::Ptr textLine = doc->plainKateTextLine( line.line() );
  1514. TQString str = textLine->string();
  1515. // find a possible start-of-comment
  1516. int p = -2; // so the first find starts at position 0
  1517. do p = str.find( "//", p + 2 );
  1518. while ( p >= 0 && textLine->attribute(p) != commentAttrib && textLine->attribute(p) != doxyCommentAttrib );
  1519. // no // found? use whole string
  1520. if ( p < 0 )
  1521. p = str.length();
  1522. // ignore trailing blanks. p starts one-past-the-end.
  1523. while( p > 0 && str[p-1].isSpace() ) --p;
  1524. return p - 1;
  1525. }
  1526. bool KateCSAndSIndent::inForStatement( int line )
  1527. {
  1528. // does this line end in a for ( ...
  1529. // with no closing ) ?
  1530. int parens = 0, semicolons = 0;
  1531. for ( ; line >= 0; --line )
  1532. {
  1533. KateTextLine::Ptr textLine = doc->plainKateTextLine(line);
  1534. const int first = textLine->firstChar();
  1535. const int last = textLine->lastChar();
  1536. // look backwards for a symbol: (){};
  1537. // match ()s, {...; and }...; => not in a for
  1538. // ; ; ; => not in a for
  1539. // ( ; and ( ; ; => a for
  1540. for ( int curr = last; curr >= first; --curr )
  1541. {
  1542. if ( textLine->attribute(curr) != symbolAttrib )
  1543. continue;
  1544. switch( textLine->getChar(curr) )
  1545. {
  1546. case ';':
  1547. if( ++semicolons > 2 )
  1548. return false;
  1549. break;
  1550. case '{': case '}':
  1551. return false;
  1552. case ')':
  1553. ++parens;
  1554. break;
  1555. case '(':
  1556. if( --parens < 0 )
  1557. return true;
  1558. break;
  1559. }
  1560. }
  1561. }
  1562. // no useful symbols before the ;?
  1563. // not in a for then
  1564. return false;
  1565. }
  1566. // is the start of the line containing 'begin' in a statement?
  1567. bool KateCSAndSIndent::inStatement( const KateDocCursor &begin )
  1568. {
  1569. // if the current line starts with an open brace, it's not a continuation.
  1570. // this happens after a function definition (which is treated as a continuation).
  1571. KateTextLine::Ptr textLine = doc->plainKateTextLine(begin.line());
  1572. const int first = textLine->firstChar();
  1573. // note that if we're being called from processChar the attribute has not yet been calculated
  1574. // should be reasonably safe to assume that unattributed {s are symbols; if the { is in a comment
  1575. // we don't want to touch it anyway.
  1576. const int attrib = textLine->attribute(first);
  1577. if( first >= 0 && (attrib == 0 || attrib == symbolAttrib) && textLine->getChar(first) == '{' )
  1578. return false;
  1579. int line;
  1580. for ( line = begin.line() - 1; line >= 0; --line )
  1581. {
  1582. textLine = doc->plainKateTextLine(line);
  1583. const int first = textLine->firstChar();
  1584. if ( first == -1 )
  1585. continue;
  1586. // starts with #: in a comment, don't care
  1587. // outside a comment: preprocessor, don't care
  1588. if ( textLine->getChar( first ) == '#' )
  1589. continue;
  1590. KateDocCursor currLine = begin;
  1591. currLine.setLine( line );
  1592. const int last = lastNonCommentChar( currLine );
  1593. if ( last < first )
  1594. continue;
  1595. // HACK: if we see a comment, assume boldly that this isn't a continuation.
  1596. // detecting comments (using attributes) is HARD, since they may have
  1597. // embedded alerts, or doxygen stuff, or just about anything. this is
  1598. // wrong, and needs fixing. note that only multi-line comments and
  1599. // single-line comments continued with \ are affected.
  1600. const int attrib = textLine->attribute(last);
  1601. if ( attrib == commentAttrib || attrib == doxyCommentAttrib )
  1602. return false;
  1603. char c = textLine->getChar(last);
  1604. // brace => not a continuation.
  1605. if ( attrib == symbolAttrib && c == '{' || c == '}' )
  1606. return false;
  1607. // ; => not a continuation, unless in a for (;;)
  1608. if ( attrib == symbolAttrib && c == ';' )
  1609. return inForStatement( line );
  1610. // found something interesting. maybe it's a label?
  1611. if ( attrib == symbolAttrib && c == ':' )
  1612. {
  1613. // the : above isn't necessarily the : in the label, eg in
  1614. // case 'x': a = b ? c :
  1615. // this will say no continuation incorrectly. but continued statements
  1616. // starting on a line with a label at the start is Bad Style (tm).
  1617. if( startsWithLabel( line ) )
  1618. {
  1619. // either starts with a label or a continuation. if the current line
  1620. // starts in a continuation, we're still in one. if not, this was
  1621. // a label, so we're not in one now. so continue to the next line
  1622. // upwards.
  1623. continue;
  1624. }
  1625. }
  1626. // any other character => in a continuation
  1627. return true;
  1628. }
  1629. // no non-comment text found before here - not a continuation.
  1630. return false;
  1631. }
  1632. TQString KateCSAndSIndent::continuationIndent( const KateDocCursor &begin )
  1633. {
  1634. if( !inStatement( begin ) )
  1635. return TQString::null;
  1636. return indentString;
  1637. }
  1638. /**
  1639. * Figure out how indented the line containing @p begin should be.
  1640. */
  1641. TQString KateCSAndSIndent::calcIndent (const KateDocCursor &begin)
  1642. {
  1643. KateTextLine::Ptr currLine = doc->plainKateTextLine(begin.line());
  1644. int currLineFirst = currLine->firstChar();
  1645. // if the line starts inside a comment, no change of indentation.
  1646. // FIXME: this unnecessarily copies the current indentation over itself.
  1647. // FIXME: on newline, this should copy from the previous line.
  1648. if ( currLineFirst >= 0 &&
  1649. (currLine->attribute(currLineFirst) == commentAttrib ||
  1650. currLine->attribute(currLineFirst) == doxyCommentAttrib) )
  1651. return currLine->string( 0, currLineFirst );
  1652. // if the line starts with # (but isn't a c# region thingy), no indentation at all.
  1653. if( currLineFirst >= 0 && currLine->getChar(currLineFirst) == '#' )
  1654. {
  1655. if( !currLine->stringAtPos( currLineFirst+1, TQString::fromLatin1("region") ) &&
  1656. !currLine->stringAtPos( currLineFirst+1, TQString::fromLatin1("endregion") ) )
  1657. return TQString::null;
  1658. }
  1659. /* Strategy:
  1660. * Look for an open bracket or brace, or a keyword opening a new scope, whichever comes latest.
  1661. * Found a brace: indent one tab in.
  1662. * Found a bracket: indent to the first non-white after it.
  1663. * Found a keyword: indent one tab in. for try, catch and switch, if newline is set, also add
  1664. * an open brace, a newline, and indent two tabs in.
  1665. */
  1666. KateDocCursor cur = begin;
  1667. int pos, openBraceCount = 0, openParenCount = 0;
  1668. bool lookingForScopeKeywords = true;
  1669. const char * const scopeKeywords[] = { "for", "do", "while", "if", "else" };
  1670. const char * const blockScopeKeywords[] = { "try", "catch", "switch" };
  1671. while (cur.gotoPreviousLine())
  1672. {
  1673. KateTextLine::Ptr textLine = doc->plainKateTextLine(cur.line());
  1674. const int lastChar = textLine->lastChar();
  1675. const int firstChar = textLine->firstChar();
  1676. // look through line backwards for interesting characters
  1677. for( pos = lastChar; pos >= firstChar; --pos )
  1678. {
  1679. if (textLine->attribute(pos) == symbolAttrib)
  1680. {
  1681. char tc = textLine->getChar (pos);
  1682. switch( tc )
  1683. {
  1684. case '(': case '[':
  1685. if( ++openParenCount > 0 )
  1686. return calcIndentInBracket( begin, cur, pos );
  1687. break;
  1688. case ')': case ']': openParenCount--; break;
  1689. case '{':
  1690. if( ++openBraceCount > 0 )
  1691. return calcIndentInBrace( begin, cur, pos );
  1692. break;
  1693. case '}': openBraceCount--; lookingForScopeKeywords = false; break;
  1694. case ';':
  1695. if( openParenCount == 0 )
  1696. lookingForScopeKeywords = false;
  1697. break;
  1698. }
  1699. }
  1700. // if we've not had a close brace or a semicolon yet, and we're at the same parenthesis level
  1701. // as the cursor, and we're at the start of a scope keyword, indent from it.
  1702. if ( lookingForScopeKeywords && openParenCount == 0 &&
  1703. textLine->attribute(pos) == keywordAttrib &&
  1704. (pos == 0 || textLine->attribute(pos-1) != keywordAttrib ) )
  1705. {
  1706. #define ARRLEN( array ) ( sizeof(array)/sizeof(array[0]) )
  1707. for( uint n = 0; n < ARRLEN(scopeKeywords); ++n )
  1708. if( textLine->stringAtPos(pos, TQString::fromLatin1(scopeKeywords[n]) ) )
  1709. return calcIndentAfterKeyword( begin, cur, pos, false );
  1710. for( uint n = 0; n < ARRLEN(blockScopeKeywords); ++n )
  1711. if( textLine->stringAtPos(pos, TQString::fromLatin1(blockScopeKeywords[n]) ) )
  1712. return calcIndentAfterKeyword( begin, cur, pos, true );
  1713. #undef ARRLEN
  1714. }
  1715. }
  1716. }
  1717. // no active { in file.
  1718. return TQString::null;
  1719. }
  1720. TQString KateCSAndSIndent::calcIndentInBracket(const KateDocCursor &indentCursor, const KateDocCursor &bracketCursor, int bracketPos)
  1721. {
  1722. KateTextLine::Ptr indentLine = doc->plainKateTextLine(indentCursor.line());
  1723. KateTextLine::Ptr bracketLine = doc->plainKateTextLine(bracketCursor.line());
  1724. // FIXME: hard-coded max indent to bracket width - use a kate variable
  1725. // FIXME: expand tabs first...
  1726. if ( bracketPos > 48 )
  1727. {
  1728. // how far to indent? we could look back for a brace or keyword, 2 from that.
  1729. // as it is, we just indent one more than the line with the ( on it.
  1730. // the potential problem with this is when
  1731. // you have code ( which does <-- continuation + start of func call
  1732. // something like this ); <-- extra indentation for func call
  1733. // then again (
  1734. // it works better than (
  1735. // the other method for (
  1736. // cases like this )));
  1737. // consequently, i think this method wins.
  1738. return indentString + initialWhitespace( bracketLine, bracketLine->firstChar() );
  1739. }
  1740. const int indentLineFirst = indentLine->firstChar();
  1741. int indentTo;
  1742. const int attrib = indentLine->attribute(indentLineFirst);
  1743. if( indentLineFirst >= 0 && (attrib == 0 || attrib == symbolAttrib) &&
  1744. ( indentLine->getChar(indentLineFirst) == ')' || indentLine->getChar(indentLineFirst) == ']' ) )
  1745. {
  1746. // If the line starts with a close bracket, line it up
  1747. indentTo = bracketPos;
  1748. }
  1749. else
  1750. {
  1751. // Otherwise, line up with the text after the open bracket
  1752. indentTo = bracketLine->nextNonSpaceChar( bracketPos + 1 );
  1753. if( indentTo == -1 )
  1754. indentTo = bracketPos + 2;
  1755. }
  1756. return initialWhitespace( bracketLine, indentTo );
  1757. }
  1758. TQString KateCSAndSIndent::calcIndentAfterKeyword(const KateDocCursor &indentCursor, const KateDocCursor &keywordCursor, int keywordPos, bool blockKeyword)
  1759. {
  1760. KateTextLine::Ptr keywordLine = doc->plainKateTextLine(keywordCursor.line());
  1761. KateTextLine::Ptr indentLine = doc->plainKateTextLine(indentCursor.line());
  1762. TQString whitespaceToKeyword = initialWhitespace( keywordLine, keywordPos, false );
  1763. if( blockKeyword ) {
  1764. // FIXME: we could add the open brace and subsequent newline here since they're definitely needed.
  1765. }
  1766. // If the line starts with an open brace, don't indent...
  1767. int first = indentLine->firstChar();
  1768. // if we're being called from processChar attribute won't be set
  1769. const int attrib = indentLine->attribute(first);
  1770. if( first >= 0 && (attrib == 0 || attrib == symbolAttrib) && indentLine->getChar(first) == '{' )
  1771. return whitespaceToKeyword;
  1772. // don't check for a continuation. rules are simple here:
  1773. // if we're in a non-compound statement after a scope keyword, we indent all lines
  1774. // once. so:
  1775. // if ( some stuff
  1776. // goes here )
  1777. // apples, and <-- continuation here is ignored. but this is Bad Style (tm) anyway.
  1778. // oranges too;
  1779. return indentString + whitespaceToKeyword;
  1780. }
  1781. TQString KateCSAndSIndent::calcIndentInBrace(const KateDocCursor &indentCursor, const KateDocCursor &braceCursor, int bracePos)
  1782. {
  1783. KateTextLine::Ptr braceLine = doc->plainKateTextLine(braceCursor.line());
  1784. const int braceFirst = braceLine->firstChar();
  1785. TQString whitespaceToOpenBrace = initialWhitespace( braceLine, bracePos, false );
  1786. // if the open brace is the start of a namespace, don't indent...
  1787. // FIXME: this is an extremely poor heuristic. it looks on the line with
  1788. // the { and the line before to see if they start with a keyword
  1789. // beginning 'namespace'. that's 99% of usage, I'd guess.
  1790. {
  1791. if( braceFirst >= 0 && braceLine->attribute(braceFirst) == keywordAttrib &&
  1792. braceLine->stringAtPos( braceFirst, TQString::fromLatin1( "namespace" ) ) )
  1793. return continuationIndent(indentCursor) + whitespaceToOpenBrace;
  1794. if( braceCursor.line() > 0 )
  1795. {
  1796. KateTextLine::Ptr prevLine = doc->plainKateTextLine(braceCursor.line() - 1);
  1797. int firstPrev = prevLine->firstChar();
  1798. if( firstPrev >= 0 && prevLine->attribute(firstPrev) == keywordAttrib &&
  1799. prevLine->stringAtPos( firstPrev, TQString::fromLatin1( "namespace" ) ) )
  1800. return continuationIndent(indentCursor) + whitespaceToOpenBrace;
  1801. }
  1802. }
  1803. KateTextLine::Ptr indentLine = doc->plainKateTextLine(indentCursor.line());
  1804. const int indentFirst = indentLine->firstChar();
  1805. // if the line starts with a close brace, don't indent...
  1806. if( indentFirst >= 0 && indentLine->getChar(indentFirst) == '}' )
  1807. return whitespaceToOpenBrace;
  1808. // if : is the first character (and not followed by another :), this is the start
  1809. // of an initialization list, or a continuation of a ?:. either way, indent twice.
  1810. if ( indentFirst >= 0 && indentLine->attribute(indentFirst) == symbolAttrib &&
  1811. indentLine->getChar(indentFirst) == ':' && indentLine->getChar(indentFirst+1) != ':' )
  1812. {
  1813. return indentString + indentString + whitespaceToOpenBrace;
  1814. }
  1815. const bool continuation = inStatement(indentCursor);
  1816. // if the current line starts with a label, don't indent...
  1817. if( !continuation && startsWithLabel( indentCursor.line() ) )
  1818. return whitespaceToOpenBrace;
  1819. // the normal case: indent once for the brace, again if it's a continuation
  1820. TQString continuationIndent = continuation ? indentString : TQString::null;
  1821. return indentString + continuationIndent + whitespaceToOpenBrace;
  1822. }
  1823. void KateCSAndSIndent::processChar(TQChar c)
  1824. {
  1825. // 'n' trigger is for c# regions.
  1826. static const TQString triggers("}{)]/:;#n");
  1827. if (triggers.find(c) == -1)
  1828. return;
  1829. // for historic reasons, processChar doesn't get a cursor
  1830. // to work on. so fabricate one.
  1831. KateView *view = doc->activeView();
  1832. KateDocCursor begin(view->cursorLine(), 0, doc);
  1833. KateTextLine::Ptr textLine = doc->plainKateTextLine(begin.line());
  1834. if ( c == 'n' )
  1835. {
  1836. int first = textLine->firstChar();
  1837. if( first < 0 || textLine->getChar(first) != '#' )
  1838. return;
  1839. }
  1840. if ( textLine->attribute( begin.col() ) == doxyCommentAttrib )
  1841. {
  1842. // dominik: if line is "* /", change it to "*/"
  1843. if ( c == '/' )
  1844. {
  1845. int first = textLine->firstChar();
  1846. // if the first char exists and is a '*', and the next non-space-char
  1847. // is already the just typed '/', concatenate it to "*/".
  1848. if ( first != -1
  1849. && textLine->getChar( first ) == '*'
  1850. && textLine->nextNonSpaceChar( first+1 ) == view->cursorColumnReal()-1 )
  1851. doc->removeText( view->cursorLine(), first+1, view->cursorLine(), view->cursorColumnReal()-1);
  1852. }
  1853. // anders: don't change the indent of doxygen lines here.
  1854. return;
  1855. }
  1856. processLine(begin);
  1857. }
  1858. //END
  1859. //BEGIN KateVarIndent
  1860. class KateVarIndentPrivate {
  1861. public:
  1862. TQRegExp reIndentAfter, reIndent, reUnindent;
  1863. TQString triggers;
  1864. uint couples;
  1865. uchar coupleAttrib;
  1866. };
  1867. KateVarIndent::KateVarIndent( KateDocument *doc )
  1868. : KateNormalIndent( doc )
  1869. {
  1870. d = new KateVarIndentPrivate;
  1871. d->reIndentAfter = TQRegExp( doc->variable( "var-indent-indent-after" ) );
  1872. d->reIndent = TQRegExp( doc->variable( "var-indent-indent" ) );
  1873. d->reUnindent = TQRegExp( doc->variable( "var-indent-unindent" ) );
  1874. d->triggers = doc->variable( "var-indent-triggerchars" );
  1875. d->coupleAttrib = 0;
  1876. slotVariableChanged( "var-indent-couple-attribute", doc->variable( "var-indent-couple-attribute" ) );
  1877. slotVariableChanged( "var-indent-handle-couples", doc->variable( "var-indent-handle-couples" ) );
  1878. // update if a setting is changed
  1879. connect( doc, TQT_SIGNAL(variableChanged( const TQString&, const TQString&) ),
  1880. this, TQT_SLOT(slotVariableChanged( const TQString&, const TQString& )) );
  1881. }
  1882. KateVarIndent::~KateVarIndent()
  1883. {
  1884. delete d;
  1885. }
  1886. void KateVarIndent::processNewline ( KateDocCursor &begin, bool /*needContinue*/ )
  1887. {
  1888. // process the line left, as well as the one entered
  1889. KateDocCursor left( begin.line()-1, 0, doc );
  1890. processLine( left );
  1891. processLine( begin );
  1892. }
  1893. void KateVarIndent::processChar ( TQChar c )
  1894. {
  1895. // process line if the c is in our list, and we are not in comment text
  1896. if ( d->triggers.contains( c ) )
  1897. {
  1898. KateTextLine::Ptr ln = doc->plainKateTextLine( doc->activeView()->cursorLine() );
  1899. if ( ln->attribute( doc->activeView()->cursorColumn()-1 ) == commentAttrib )
  1900. return;
  1901. KateView *view = doc->activeView();
  1902. KateDocCursor begin( view->cursorLine(), 0, doc );
  1903. kdDebug(13030)<<"variable indenter: process char '"<<c<<", line "<<begin.line()<<endl;
  1904. processLine( begin );
  1905. }
  1906. }
  1907. void KateVarIndent::processLine ( KateDocCursor &line )
  1908. {
  1909. TQString indent; // store the indent string here
  1910. // find the first line with content that is not starting with comment text,
  1911. // and take the position from that
  1912. int ln = line.line();
  1913. int pos = -1;
  1914. KateTextLine::Ptr ktl = doc->plainKateTextLine( ln );
  1915. if ( ! ktl ) return; // no line!?
  1916. // skip blank lines, except for the cursor line
  1917. KateView *v = doc->activeView();
  1918. if ( (ktl->firstChar() < 0) && (!v || (int)v->cursorLine() != ln ) )
  1919. return;
  1920. int fc;
  1921. if ( ln > 0 )
  1922. do
  1923. {
  1924. ktl = doc->plainKateTextLine( --ln );
  1925. fc = ktl->firstChar();
  1926. if ( ktl->attribute( fc ) != commentAttrib )
  1927. pos = fc;
  1928. }
  1929. while ( (ln > 0) && (pos < 0) ); // search a not empty text line
  1930. if ( pos < 0 )
  1931. pos = 0;
  1932. else
  1933. pos = ktl->cursorX( pos, tabWidth );
  1934. int adjustment = 0;
  1935. // try 'couples' for an opening on the above line first. since we only adjust by 1 unit,
  1936. // we only need 1 match.
  1937. if ( d->couples & Parens && coupleBalance( ln, '(', ')' ) > 0 )
  1938. adjustment++;
  1939. else if ( d->couples & Braces && coupleBalance( ln, '{', '}' ) > 0 )
  1940. adjustment++;
  1941. else if ( d->couples & Brackets && coupleBalance( ln, '[', ']' ) > 0 )
  1942. adjustment++;
  1943. // Try 'couples' for a closing on this line first. since we only adjust by 1 unit,
  1944. // we only need 1 match. For unindenting, we look for a closing character
  1945. // *at the beginning of the line*
  1946. // NOTE Assume that a closing brace with the configured attribute on the start
  1947. // of the line is closing.
  1948. // When acting on processChar, the character isn't highlighted. So I could
  1949. // either not check, assuming that the first char *is* meant to close, or do a
  1950. // match test if the attrib is 0. How ever, doing that is
  1951. // a potentially huge job, if the match is several hundred lines away.
  1952. // Currently, the check is done.
  1953. {
  1954. KateTextLine::Ptr tl = doc->plainKateTextLine( line.line() );
  1955. int i = tl->firstChar();
  1956. if ( i > -1 )
  1957. {
  1958. TQChar ch = tl->getChar( i );
  1959. uchar at = tl->attribute( i );
  1960. kdDebug(13030)<<"attrib is "<<at<<endl;
  1961. if ( d->couples & Parens && ch == ')'
  1962. && ( at == d->coupleAttrib
  1963. || (! at && hasRelevantOpening( KateDocCursor( line.line(), i, doc ) ))
  1964. )
  1965. )
  1966. adjustment--;
  1967. else if ( d->couples & Braces && ch == '}'
  1968. && ( at == d->coupleAttrib
  1969. || (! at && hasRelevantOpening( KateDocCursor( line.line(), i, doc ) ))
  1970. )
  1971. )
  1972. adjustment--;
  1973. else if ( d->couples & Brackets && ch == ']'
  1974. && ( at == d->coupleAttrib
  1975. || (! at && hasRelevantOpening( KateDocCursor( line.line(), i, doc ) ))
  1976. )
  1977. )
  1978. adjustment--;
  1979. }
  1980. }
  1981. #define ISCOMMENTATTR(attr) (attr==commentAttrib||attr==doxyCommentAttrib)
  1982. #define ISCOMMENT (ISCOMMENTATTR(ktl->attribute(ktl->firstChar()))||ISCOMMENTATTR(ktl->attribute(matchpos)))
  1983. // check if we should indent, unless the line starts with comment text,
  1984. // or the match is in comment text
  1985. kdDebug(13030)<<"variable indenter: starting indent: "<<pos<<endl;
  1986. // check if the above line indicates that we shuld add indentation
  1987. int matchpos = 0;
  1988. if ( ktl && ! d->reIndentAfter.isEmpty()
  1989. && (matchpos = d->reIndentAfter.search( doc->textLine( ln ) )) > -1
  1990. && ! ISCOMMENT )
  1991. adjustment++;
  1992. // else, check if this line should indent unless ...
  1993. ktl = doc->plainKateTextLine( line.line() );
  1994. if ( ! d->reIndent.isEmpty()
  1995. && (matchpos = d->reIndent.search( doc->textLine( line.line() ) )) > -1
  1996. && ! ISCOMMENT )
  1997. adjustment++;
  1998. // else, check if the current line indicates if we should remove indentation unless ...
  1999. if ( ! d->reUnindent.isEmpty()
  2000. && (matchpos = d->reUnindent.search( doc->textLine( line.line() ) )) > -1
  2001. && ! ISCOMMENT )
  2002. adjustment--;
  2003. kdDebug(13030)<<"variable indenter: adjusting by "<<adjustment<<" units"<<endl;
  2004. if ( adjustment > 0 )
  2005. pos += indentWidth;
  2006. else if ( adjustment < 0 )
  2007. pos -= indentWidth;
  2008. ln = line.line();
  2009. fc = doc->plainKateTextLine( ln )->firstChar();
  2010. // dont change if there is no change.
  2011. // ### should I actually compare the strings?
  2012. // FIXME for some odd reason, the document gets marked as changed
  2013. // even if we don't change it !?
  2014. if ( fc == pos )
  2015. return;
  2016. if ( fc > 0 )
  2017. doc->removeText (ln, 0, ln, fc );
  2018. if ( pos > 0 )
  2019. indent = tabString( pos );
  2020. if ( pos > 0 )
  2021. doc->insertText (ln, 0, indent);
  2022. // try to restore cursor ?
  2023. line.setCol( pos );
  2024. }
  2025. void KateVarIndent::processSection (const KateDocCursor &begin, const KateDocCursor &end)
  2026. {
  2027. KateDocCursor cur = begin;
  2028. while (cur.line() <= end.line())
  2029. {
  2030. processLine (cur);
  2031. if (!cur.gotoNextLine())
  2032. break;
  2033. }
  2034. }
  2035. void KateVarIndent::slotVariableChanged( const TQString &var, const TQString &val )
  2036. {
  2037. if ( ! var.startsWith("var-indent") )
  2038. return;
  2039. if ( var == "var-indent-indent-after" )
  2040. d->reIndentAfter.setPattern( val );
  2041. else if ( var == "var-indent-indent" )
  2042. d->reIndent.setPattern( val );
  2043. else if ( var == "var-indent-unindent" )
  2044. d->reUnindent.setPattern( val );
  2045. else if ( var == "var-indent-triggerchars" )
  2046. d->triggers = val;
  2047. else if ( var == "var-indent-handle-couples" )
  2048. {
  2049. d->couples = 0;
  2050. TQStringList l = TQStringList::split( " ", val );
  2051. if ( l.contains("parens") ) d->couples |= Parens;
  2052. if ( l.contains("braces") ) d->couples |= Braces;
  2053. if ( l.contains("brackets") ) d->couples |= Brackets;
  2054. }
  2055. else if ( var == "var-indent-couple-attribute" )
  2056. {
  2057. //read a named attribute of the config.
  2058. KateHlItemDataList items;
  2059. doc->highlight()->getKateHlItemDataListCopy (0, items);
  2060. for (uint i=0; i<items.count(); i++)
  2061. {
  2062. if ( items.at(i)->name.section( ':', 1 ) == val )
  2063. {
  2064. d->coupleAttrib = i;
  2065. break;
  2066. }
  2067. }
  2068. }
  2069. }
  2070. int KateVarIndent::coupleBalance ( int line, const TQChar &open, const TQChar &close ) const
  2071. {
  2072. int r = 0;
  2073. KateTextLine::Ptr ln = doc->plainKateTextLine( line );
  2074. if ( ! ln || ! ln->length() ) return 0;
  2075. for ( uint z=0; z < ln->length(); z++ )
  2076. {
  2077. TQChar c = ln->getChar( z );
  2078. if ( ln->attribute(z) == d->coupleAttrib )
  2079. {
  2080. kdDebug(13030)<<z<<", "<<c<<endl;
  2081. if (c == open)
  2082. r++;
  2083. else if (c == close)
  2084. r--;
  2085. }
  2086. }
  2087. return r;
  2088. }
  2089. bool KateVarIndent::hasRelevantOpening( const KateDocCursor &end ) const
  2090. {
  2091. KateDocCursor cur = end;
  2092. int count = 1;
  2093. TQChar close = cur.currentChar();
  2094. TQChar opener;
  2095. if ( close == '}' ) opener = '{';
  2096. else if ( close = ')' ) opener = '(';
  2097. else if (close = ']' ) opener = '[';
  2098. else return false;
  2099. //Move backwards 1 by 1 and find the opening partner
  2100. while (cur.moveBackward(1))
  2101. {
  2102. if (cur.currentAttrib() == d->coupleAttrib)
  2103. {
  2104. TQChar ch = cur.currentChar();
  2105. if (ch == opener)
  2106. count--;
  2107. else if (ch == close)
  2108. count++;
  2109. if (count == 0)
  2110. return true;
  2111. }
  2112. }
  2113. return false;
  2114. }
  2115. //END KateVarIndent
  2116. //BEGIN KateScriptIndent
  2117. KateScriptIndent::KateScriptIndent( KateDocument *doc )
  2118. : KateNormalIndent( doc )
  2119. {
  2120. m_script=KateFactory::self()->indentScript ("script-indent-c1-test");
  2121. }
  2122. KateScriptIndent::~KateScriptIndent()
  2123. {
  2124. }
  2125. void KateScriptIndent::processNewline( KateDocCursor &begin, bool needContinue )
  2126. {
  2127. kdDebug(13030) << "processNewline" << endl;
  2128. KateView *view = doc->activeView();
  2129. if (view)
  2130. {
  2131. TQString errorMsg;
  2132. TQTime t;
  2133. t.start();
  2134. kdDebug(13030)<<"calling m_script.processChar"<<endl;
  2135. if( !m_script.processNewline( view, begin, needContinue , errorMsg ) )
  2136. {
  2137. kdDebug(13030) << "Error in script-indent: " << errorMsg << endl;
  2138. }
  2139. kdDebug(13030) << "ScriptIndent::TIME in ms: " << t.elapsed() << endl;
  2140. }
  2141. }
  2142. void KateScriptIndent::processChar( TQChar c )
  2143. {
  2144. kdDebug(13030) << "processChar" << endl;
  2145. KateView *view = doc->activeView();
  2146. if (view)
  2147. {
  2148. TQString errorMsg;
  2149. TQTime t;
  2150. t.start();
  2151. kdDebug(13030)<<"calling m_script.processChar"<<endl;
  2152. if( !m_script.processChar( view, c , errorMsg ) )
  2153. {
  2154. kdDebug(13030) << "Error in script-indent: " << errorMsg << endl;
  2155. }
  2156. kdDebug(13030) << "ScriptIndent::TIME in ms: " << t.elapsed() << endl;
  2157. }
  2158. }
  2159. void KateScriptIndent::processLine (KateDocCursor &line)
  2160. {
  2161. kdDebug(13030) << "processLine" << endl;
  2162. KateView *view = doc->activeView();
  2163. if (view)
  2164. {
  2165. TQString errorMsg;
  2166. TQTime t;
  2167. t.start();
  2168. kdDebug(13030)<<"calling m_script.processLine"<<endl;
  2169. if( !m_script.processLine( view, line , errorMsg ) )
  2170. {
  2171. kdDebug(13030) << "Error in script-indent: " << errorMsg << endl;
  2172. }
  2173. kdDebug(13030) << "ScriptIndent::TIME in ms: " << t.elapsed() << endl;
  2174. }
  2175. }
  2176. //END KateScriptIndent
  2177. //BEGIN ScriptIndentConfigPage, THIS IS ONLY A TEST! :)
  2178. #include <tqlabel.h>
  2179. ScriptIndentConfigPage::ScriptIndentConfigPage ( TQWidget *parent, const char *name )
  2180. : IndenterConfigPage(parent, name)
  2181. {
  2182. TQLabel* hello = new TQLabel("Hello world! Dummy for testing purpose.", this);
  2183. hello->show();
  2184. }
  2185. ScriptIndentConfigPage::~ScriptIndentConfigPage ()
  2186. {
  2187. }
  2188. void ScriptIndentConfigPage::apply ()
  2189. {
  2190. kdDebug(13030) << "ScriptIndentConfigPagE::apply() was called, save config options now!" << endl;
  2191. }
  2192. //END ScriptIndentConfigPage
  2193. // kate: space-indent on; indent-width 2; replace-tabs on;