Files
Sourcetrail/src/lib/data/location/TokenLocation.cpp
T
malte_langkabel fd73abde64 ui: snippet sizes
* added SnippetMerger class that conducts the merging and snapping of TokenLocations to create snippets.
* added parameters for snippet snapping and merging to ApplicationSettings.
* added forEachStartTokenLocation() and forEachEndTokenLocation() to TokenLocationLine and TokenLocationFile.
* added < and > operators to TokenLocation that compare the location part (not the id).
2015-05-04 15:00:18 +02:00

199 lines
3.3 KiB
C++

#include "data/location/TokenLocation.h"
#include "data/location/TokenLocationLine.h"
TokenLocation::TokenLocation(Id tokenId, TokenLocationLine* line, unsigned int columnNumber, bool isStart)
: m_id(s_locationId++)
, m_tokenId(tokenId)
, m_type(LOCATION_TOKEN)
, m_line(line)
, m_columnNumber(columnNumber)
, m_other(nullptr)
, m_isStart(isStart)
{
}
TokenLocation::TokenLocation(TokenLocation *other, TokenLocationLine* line, unsigned int columnNumber, bool isStart)
: m_id(other->m_id)
, m_tokenId(other->m_tokenId)
, m_type(other->m_type)
, m_line(line)
, m_columnNumber(columnNumber)
, m_other(other)
, m_isStart(isStart)
{
}
TokenLocation::TokenLocation(const TokenLocation& other, TokenLocationLine* line)
: m_id(other.m_id)
, m_tokenId(other.m_tokenId)
, m_type(other.m_type)
, m_line(line)
, m_columnNumber(other.m_columnNumber)
, m_other(nullptr)
, m_isStart(other.m_isStart)
{
}
TokenLocation::~TokenLocation()
{
}
bool TokenLocation::operator<(const TokenLocation& rhs) const
{
return (
getLineNumber() < rhs.getLineNumber() || (
getLineNumber() == rhs.getLineNumber() &&
getColumnNumber() < rhs.getColumnNumber()
)
);
}
bool TokenLocation::operator>(const TokenLocation& rhs) const
{
return (
getLineNumber() > rhs.getLineNumber() || (
getLineNumber() == rhs.getLineNumber() &&
getColumnNumber() > rhs.getColumnNumber()
)
);
}
Id TokenLocation::getId() const
{
return m_id;
}
Id TokenLocation::getTokenId() const
{
return m_tokenId;
}
TokenLocation::LocationType TokenLocation::getType() const
{
return m_type;
}
void TokenLocation::setType(LocationType type)
{
m_type = type;
if (m_other)
{
m_other->m_type = type;
}
}
TokenLocationLine* TokenLocation::getTokenLocationLine() const
{
return m_line;
}
TokenLocationFile* TokenLocation::getTokenLocationFile() const
{
return m_line->getTokenLocationFile();
}
unsigned int TokenLocation::getColumnNumber() const
{
return m_columnNumber;
}
unsigned int TokenLocation::getLineNumber() const
{
return m_line->getLineNumber();
}
const FilePath& TokenLocation::getFilePath() const
{
return m_line->getFilePath();
}
TokenLocation* TokenLocation::getOtherTokenLocation() const
{
return m_other;
}
void TokenLocation::setOtherTokenLocation(TokenLocation* location)
{
m_other = location;
}
TokenLocation* TokenLocation::getStartTokenLocation()
{
if (m_isStart)
{
return this;
}
else
{
return m_other;
}
}
TokenLocation* TokenLocation::getEndTokenLocation()
{
if (!m_isStart)
{
return this;
}
else
{
return m_other;
}
}
const TokenLocation* TokenLocation::getStartTokenLocation() const
{
if (m_isStart)
{
return this;
}
else
{
return m_other;
}
}
const TokenLocation* TokenLocation::getEndTokenLocation() const
{
if (!m_isStart)
{
return this;
}
else
{
return m_other;
}
}
bool TokenLocation::isStartTokenLocation() const
{
return m_isStart;
}
bool TokenLocation::isEndTokenLocation() const
{
return !m_isStart;
}
Id TokenLocation::s_locationId = 1;
std::ostream& operator<<(std::ostream& ostream, const TokenLocation& location)
{
if ((&location)->isStartTokenLocation())
{
ostream << "<";
}
ostream << location.getColumnNumber() << ":[" << location.getTokenId() << "]";
if ((&location)->isEndTokenLocation())
{
ostream << ">";
}
ostream << " ";
return ostream;
}