minor fixes
This commit is contained in:
parent
60eaba951a
commit
0bc67209b9
4 changed files with 24 additions and 14 deletions
79
src/indent_stack.hpp
Normal file
79
src/indent_stack.hpp
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
#pragma once
|
||||
#include <stack>
|
||||
enum class IndentAction
|
||||
{
|
||||
Indent,
|
||||
Dedent,
|
||||
None
|
||||
};
|
||||
|
||||
constexpr int TAB_WIDTH = 8;
|
||||
|
||||
class IndentStack
|
||||
{
|
||||
std::stack<int> indent_stack;
|
||||
int pending_dedents = 0;
|
||||
|
||||
public:
|
||||
IndentStack() { indent_stack.push(0); }
|
||||
|
||||
IndentAction updateIndentation(int spaces)
|
||||
{
|
||||
int current = indent_stack.top();
|
||||
|
||||
if (spaces == current)
|
||||
{
|
||||
return IndentAction::None;
|
||||
}
|
||||
|
||||
if (spaces > current)
|
||||
{
|
||||
indent_stack.push(spaces);
|
||||
return IndentAction::Indent;
|
||||
}
|
||||
|
||||
while (indent_stack.top() > spaces)
|
||||
{
|
||||
indent_stack.pop();
|
||||
pending_dedents++;
|
||||
}
|
||||
|
||||
if (pending_dedents > 0)
|
||||
{
|
||||
pending_dedents--;
|
||||
return IndentAction::Dedent;
|
||||
}
|
||||
|
||||
return IndentAction::None;
|
||||
}
|
||||
|
||||
bool popDedent()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
bool hasRemainingDedents()
|
||||
{
|
||||
return pending_dedents > 0;
|
||||
}
|
||||
|
||||
IndentAction closeBlock()
|
||||
{
|
||||
return updateIndentation(0);
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue