CE Code Col Line.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /******************************************************************************/
  2. #if EE_PRIVATE
  3. namespace Edit{
  4. /******************************************************************************/
  5. struct CodeCol // Code Column
  6. {
  7. Char c ; // character
  8. TOKEN_TYPE type ;
  9. VecI2 pos ; // original position
  10. Int token; // original token index
  11. CodeCol& set (Char c, Int col, Int line, Int token, TOKEN_TYPE type) {T.c=c; T.pos.set(col, line); T.token=token; T.type=type; return T;}
  12. CodeCol& remove( ) {return set(' ', -1, -1, -1, TOKEN_REMOVE);}
  13. };
  14. /******************************************************************************/
  15. struct CodeLine // Code Line
  16. {
  17. VecI2 lines; // original line index range (x=from, y=to, can be invalid "y<x")
  18. Memc<CodeCol> cols ; // columns
  19. Bool hasLine ( Int l)C {return l >=lines.x && l <=lines.y;}
  20. Bool hasAnyLines(C VecI2 &l)C {return l.y>=lines.x && l.x<=lines.y;} // if has any of these lines
  21. CodeLine& includeLine ( Int l) {if(lines.y<lines.x)lines=l;else if(l <lines.x)lines.x=l ;else if(l >lines.y)lines.y=l ; return T;}
  22. CodeLine& includeLines (C VecI2 &l) {if(lines.y<lines.x)lines=l;else{if(l.x<lines.x)lines.x=l.x; if(l.y>lines.y)lines.y=l.y;} return T;}
  23. Char operator[](Int i)C {return InRange(i, cols) ? cols[i].c : '\0';}
  24. TOKEN_TYPE type (Int i)C {return InRange(i, cols) ? cols[i].type : TOKEN_NONE;}
  25. Str asStr()C {Str s; s.reserve(cols.elms()); FREPA(cols)s+=cols[i].c; return s;}
  26. Int length ()C {return cols.elms();}
  27. Bool empty ()C {REPA(cols)if(ValidType(cols[i].type))return false; return true;}
  28. Bool toRemove()C
  29. {
  30. Bool has_remove=false; REPA(cols){TOKEN_TYPE t=cols[i].type; if(t==TOKEN_REMOVE)has_remove=true; if(ValidType(t))return false;}
  31. return has_remove;
  32. }
  33. Int findPos (C VecI2 &pos )C {REPA(cols)if(cols[i].pos==pos)return i; return -1;}
  34. Int findToken( Int token, Bool start=true)C
  35. {
  36. if(token>=0)
  37. {
  38. if(start){FREPA(cols)if(cols[i].token==token)return i;}
  39. else { REPA(cols)if(cols[i].token==token)return i;}
  40. }
  41. return -1;
  42. }
  43. CodeLine& remove(Int i ) {cols.remove(i, true); return T;}
  44. CodeLine& insert(Int i, Char c , TOKEN_TYPE type, Int token=-1) {cols.NewAt(i).set(c, -1, -1, token, type); return T;}
  45. CodeLine& insert(Int i, C Str &text, TOKEN_TYPE type, Int token=-1) {FREPAD(t, text)insert(i+t , text[t], type, token); return T;}
  46. CodeLine& append( Char c , TOKEN_TYPE type, Int token=-1) {return insert(cols.elms(), c , type, token);}
  47. CodeLine& append( C Str &text, TOKEN_TYPE type, Int token=-1) {return insert(cols.elms(), text , type, token);}
  48. Bool operator==(C Str &text)C;
  49. Bool isKeyword (Int i, CChar8 *keyword)C;
  50. Bool starts (Int i, CChar8 *text )C;
  51. virtual ~CodeLine() {} // force virtual class to enable memory container auto-casting when extending this class with another virtual class, this is needed for 'ViewLine'
  52. CodeLine() {lines.set(0, -1);} // set invalid at start
  53. };
  54. /******************************************************************************/
  55. struct SourceLoc // Source Location
  56. {
  57. Bool file; // if true then this is a file stored on disk (using 'file_name'), if false then this is a file stored in project (using 'id')
  58. Str file_name, base_name; // 'file_name' full path+name of the file, 'base_name' helper used for find functionality
  59. UID id;
  60. static Int Compare(C SourceLoc &a, C SourceLoc &b);
  61. // get
  62. Str asText()C;
  63. Bool is ()C {return file ? file_name.is() : id.valid();}
  64. Bool operator==(C SourceLoc &loc)C {return (file!=loc.file) ? false : file ? EqualPath(file_name, loc.file_name) : (id==loc.id);}
  65. Bool operator!=(C SourceLoc &loc)C {return !(T==loc);}
  66. // operations
  67. void clear ( ) {file=false; file_name.clear(); base_name.clear(); id.zero();}
  68. void setFile(C Str &name) {file=true ; file_name=Replace(name, '/', '\\'); T.id.zero(); base_name=GetBaseNoExt(file_name);}
  69. void setID (C UID &id ) {file=false; file_name.clear(); T.id=id ; base_name=GetBaseNoExt(asText() );}
  70. void replacePath(C Str &src, C Str &dest) {if(file)ReplacePath(file_name, src, dest);}
  71. SourceLoc( ) {clear();}
  72. SourceLoc(C Str &file) {clear(); setFile(file);}
  73. SourceLoc(C UID &id ) {clear(); setID (id );}
  74. };
  75. /******************************************************************************/
  76. struct LineMap
  77. {
  78. struct Section
  79. {
  80. struct Map
  81. {
  82. Int line_index;
  83. UID line_id;
  84. };
  85. SourceLoc src;
  86. Int offset;
  87. Memc<Map> target_to_original; // mapping from target line (offsetted by 'offset') to original line in 'src'
  88. };
  89. Memc<Section> sections;
  90. void get(Int line, SourceLoc &src, Int &original_index, UID &original_id)C; // get original location
  91. void add(C SourceLoc &src, C Memc<CodeLine> &lines); // add new section
  92. void operator++(int);
  93. Bool load(C Str &file) {return true;} // don't delete existing data in case we're accessing the map to add a new section
  94. };
  95. /******************************************************************************/
  96. Int FindLineI(Memc<CodeLine> &code_lines, Int line);
  97. CodeLine* FindLine (Memc<CodeLine> &code_lines, Int line);
  98. CodeLine* FindLineCol(Memc<CodeLine> &code_lines, C VecI2 &pos , Int &cl_col);
  99. Bool FindLineCol(Memc<CodeLine> &code_lines, C VecI2 &pos , VecI2 &cl_pos);
  100. Bool FindLineCol(Memc<CodeLine> &code_lines, Int token_index, Int token_line, Bool token_start, VecI2 &cl_pos);
  101. void Remove(Memc<CodeLine> &lines, C VecI2 &start, C VecI2 &end, Bool definite);
  102. void Clean (Memc<CodeLine> &lines);
  103. void Write(FileText &f, C Memc<CodeLine> &lines);
  104. void AdjustNameSymbol(Memc<CodeLine> &lines, Symbol* &src, Symbol *dest);
  105. void Parse(Memc<CodeLine> &lines);
  106. Bool OverwriteOnChange(File &src, C Str &dest, Bool *changed=null);
  107. Bool OverwriteOnChange(FileText &src, C Str &dest, Bool *changed=null);
  108. Bool OverwriteOnChange(XmlData &src, C Str &dest, Bool params_in_separate_lines=false, ENCODING encoding=UTF_8);
  109. Bool OverwriteOnChange(Memc<CodeLine> &src, C Str &dest);
  110. Bool OverwriteOnChangeLoud(File &src, C Str &dest);
  111. Bool OverwriteOnChangeLoud(FileText &src, C Str &dest);
  112. Bool OverwriteOnChangeLoud(XmlData &src, C Str &dest, Bool params_in_separate_lines=false, ENCODING encoding=UTF_8);
  113. Bool OverwriteOnChangeLoud(Memc<CodeLine> &src, C Str &dest);
  114. /******************************************************************************/
  115. } // namespace
  116. /******************************************************************************/
  117. inline Int Elms(C Edit::CodeLine &cl) {return cl.cols.elms();} // specify in EE namespace
  118. /******************************************************************************/
  119. #endif
  120. /******************************************************************************/