// See License.txt for copyright and terms of use

#include "squash_min_max_line_visitor.h"
#include "cppundolog.h"
#include <sstream>

MinMaxLineVisitor::MinMaxLineVisitor() :
  minCol(INT_MAX), maxCol(0), minLine(INT_MAX), maxLine(0){
} 

MinMaxLineVisitor::MinMaxLineVisitor(MinMaxLineVisitor const &m)
{
  this->operator=(m);
}

MinMaxLineVisitor& MinMaxLineVisitor::operator=(MinMaxLineVisitor const &m) {
  minCol = m.minCol;
  maxCol = m.maxCol;
  minLine = m.minLine;
  maxLine = m.maxLine;
  file = m.file;
  return *this;
}

void MinMaxLineVisitor::reset() {
  maxLine = 0;
  minLine = INT_MAX;
  minCol = 0;
  file.clear();
}

void MinMaxLineVisitor::notice(SourceLoc loc, SourceLoc endloc) {
  if (loc == SL_UNKNOWN) return;
  CPPSourceLoc csl(loc);
  loc = csl.loc();
  StringRef file(NULL);
  int line(0);
  int col(0);
  sourceLocManager->decodeLineCol(loc, file, line, col);
  if (file != this->file) this->file = file;
  

  minLine = min(minLine, (unsigned int)line);
  maxLine = max(maxLine, (unsigned int)line);
  //maxCol = max(maxCol, col);
  minCol = min(minCol, (unsigned int)max(col - 1, 0));
}

std::string MinMaxLineVisitor::toString() const {
  std::stringstream ss;
  ss << file << ":" <<minCol<< ":"<< minLine << "<=" << maxLine;
  return ss.str();
}

unsigned int MinMaxLineVisitor::getIndent() const {
  xassert(minCol != INT_MAX);
  return minCol;
}

void MinMaxLineVisitor::setIndent(unsigned int col) {
  minCol = col;
}

std::string MinMaxLineVisitor::getFile() const {
  return file;
}
