minor fixes
This commit is contained in:
parent
60eaba951a
commit
0bc67209b9
4 changed files with 24 additions and 14 deletions
|
|
@ -1,71 +0,0 @@
|
|||
#pragma once
|
||||
#include <stack>
|
||||
enum class IndentAction
|
||||
{
|
||||
Indent,
|
||||
Dedent,
|
||||
None
|
||||
};
|
||||
|
||||
constexpr int TAB_WIDTH = 8;
|
||||
|
||||
class IndentHelper
|
||||
{
|
||||
std::stack<int> indent_stack;
|
||||
int pending_dedents = 0;
|
||||
|
||||
public:
|
||||
IndentHelper() { indent_stack.push(0); }
|
||||
|
||||
IndentAction processLine(int spaces)
|
||||
{
|
||||
int current = indent_stack.top();
|
||||
|
||||
if (spaces == current)
|
||||
{
|
||||
return IndentAction::None;
|
||||
}
|
||||
|
||||
if (spaces > current)
|
||||
{
|
||||
indent_stack.push(spaces);
|
||||
return IndentAction::Indent;
|
||||
}
|
||||
|
||||
pending_dedents = 0;
|
||||
while (indent_stack.top() > spaces)
|
||||
{
|
||||
indent_stack.pop();
|
||||
pending_dedents++;
|
||||
}
|
||||
|
||||
if (pending_dedents > 0)
|
||||
{
|
||||
pending_dedents--;
|
||||
return IndentAction::Dedent;
|
||||
}
|
||||
|
||||
return IndentAction::None;
|
||||
}
|
||||
|
||||
// returns false on failure
|
||||
bool consumeDedent()
|
||||
{
|
||||
if (pending_dedents <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
pending_dedents--;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int calculateColumn(const char *text, int length)
|
||||
{
|
||||
int spaces = 0;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
spaces += (text[i] == '\t') ? TAB_WIDTH : 1;
|
||||
}
|
||||
return spaces;
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue