Accessor.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Scintilla source code edit control
  2. /** @file Accessor.cxx
  3. ** Interfaces between Scintilla and lexers.
  4. **/
  5. // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
  6. // The License.txt file describes the conditions under which this software may be distributed.
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <assert.h>
  12. #include <ctype.h>
  13. #include "ILexer.h"
  14. #include "Scintilla.h"
  15. #include "SciLexer.h"
  16. #include "PropSetSimple.h"
  17. #include "WordList.h"
  18. #include "LexAccessor.h"
  19. #include "Accessor.h"
  20. #ifdef SCI_NAMESPACE
  21. using namespace Scintilla;
  22. #endif
  23. Accessor::Accessor(IDocument *pAccess_, PropSetSimple *pprops_) : LexAccessor(pAccess_), pprops(pprops_) {
  24. }
  25. int Accessor::GetPropertyInt(const char *key, int defaultValue) const {
  26. return pprops->GetInt(key, defaultValue);
  27. }
  28. int Accessor::IndentAmount(Sci_Position line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) {
  29. Sci_Position end = Length();
  30. int spaceFlags = 0;
  31. // Determines the indentation level of the current line and also checks for consistent
  32. // indentation compared to the previous line.
  33. // Indentation is judged consistent when the indentation whitespace of each line lines
  34. // the same or the indentation of one line is a prefix of the other.
  35. Sci_Position pos = LineStart(line);
  36. char ch = (*this)[pos];
  37. int indent = 0;
  38. bool inPrevPrefix = line > 0;
  39. Sci_Position posPrev = inPrevPrefix ? LineStart(line-1) : 0;
  40. while ((ch == ' ' || ch == '\t') && (pos < end)) {
  41. if (inPrevPrefix) {
  42. char chPrev = (*this)[posPrev++];
  43. if (chPrev == ' ' || chPrev == '\t') {
  44. if (chPrev != ch)
  45. spaceFlags |= wsInconsistent;
  46. } else {
  47. inPrevPrefix = false;
  48. }
  49. }
  50. if (ch == ' ') {
  51. spaceFlags |= wsSpace;
  52. indent++;
  53. } else { // Tab
  54. spaceFlags |= wsTab;
  55. if (spaceFlags & wsSpace)
  56. spaceFlags |= wsSpaceTab;
  57. indent = (indent / 8 + 1) * 8;
  58. }
  59. ch = (*this)[++pos];
  60. }
  61. *flags = spaceFlags;
  62. indent += SC_FOLDLEVELBASE;
  63. // if completely empty line or the start of a comment...
  64. if ((LineStart(line) == Length()) || (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') ||
  65. (pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)))
  66. return indent | SC_FOLDLEVELWHITEFLAG;
  67. else
  68. return indent;
  69. }