Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 216 lines (194 sloc) 5.841 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
/** @file RunStyles.cxx
** Data structure used to store sparse styles.
**/
// Copyright 1998-2007 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

#include "Platform.h"

#include "Scintilla.h"
#include "SplitVector.h"
#include "Partitioning.h"
#include "RunStyles.h"

#ifdef SCI_NAMESPACE
using namespace Scintilla;
#endif

// Find the first run at a position
int RunStyles::RunFromPosition(int position) {
int run = starts->PartitionFromPosition(position);
// Go to first element with this position
while ((run > 0) && (position == starts->PositionFromPartition(run-1))) {
run--;
}
return run;
}

// If there is no run boundary at position, insert one continuing style.
int RunStyles::SplitRun(int position) {
int run = RunFromPosition(position);
int posRun = starts->PositionFromPartition(run);
if (posRun < position) {
int runStyle = ValueAt(position);
run++;
starts->InsertPartition(run, position);
styles->InsertValue(run, 1, runStyle);
}
return run;
}

void RunStyles::RemoveRun(int run) {
starts->RemovePartition(run);
styles->DeleteRange(run, 1);
}

void RunStyles::RemoveRunIfEmpty(int run) {
if ((run < starts->Partitions()) && (starts->Partitions() > 1)) {
if (starts->PositionFromPartition(run) == starts->PositionFromPartition(run+1)) {
RemoveRun(run);
}
}
}

void RunStyles::RemoveRunIfSameAsPrevious(int run) {
if ((run > 0) && (run < starts->Partitions())) {
if (styles->ValueAt(run-1) == styles->ValueAt(run)) {
RemoveRun(run);
}
}
}

RunStyles::RunStyles() {
starts = new Partitioning(8);
styles = new SplitVector<int>();
styles->InsertValue(0, 2, 0);
}

RunStyles::~RunStyles() {
delete starts;
starts = NULL;
delete styles;
styles = NULL;
}

int RunStyles::Length() const {
return starts->PositionFromPartition(starts->Partitions());
}

int RunStyles::ValueAt(int position) const {
return styles->ValueAt(starts->PartitionFromPosition(position));
}

int RunStyles::FindNextChange(int position, int end) {
int run = starts->PartitionFromPosition(position);
if (run < starts->Partitions()) {
int runChange = starts->PositionFromPartition(run);
if (runChange > position)
return runChange;
int nextChange = starts->PositionFromPartition(run + 1);
if (nextChange > position) {
return nextChange;
} else if (position < end) {
return end;
} else {
return end + 1;
}
} else {
return end + 1;
}
}

int RunStyles::StartRun(int position) {
return starts->PositionFromPartition(starts->PartitionFromPosition(position));
}

int RunStyles::EndRun(int position) {
return starts->PositionFromPartition(starts->PartitionFromPosition(position) + 1);
}

bool RunStyles::FillRange(int &position, int value, int &fillLength) {
int end = position + fillLength;
int runEnd = RunFromPosition(end);
if (styles->ValueAt(runEnd) == value) {
// End already has value so trim range.
end = starts->PositionFromPartition(runEnd);
if (position >= end) {
// Whole range is already same as value so no action
return false;
}
fillLength = end - position;
} else {
runEnd = SplitRun(end);
}
int runStart = RunFromPosition(position);
if (styles->ValueAt(runStart) == value) {
// Start is in expected value so trim range.
runStart++;
position = starts->PositionFromPartition(runStart);
fillLength = end - position;
} else {
if (starts->PositionFromPartition(runStart) < position) {
runStart = SplitRun(position);
runEnd++;
}
}
if (runStart < runEnd) {
styles->SetValueAt(runStart, value);
// Remove each old run over the range
for (int run=runStart+1; run<runEnd; run++) {
RemoveRun(runStart+1);
}
runEnd = RunFromPosition(end);
RemoveRunIfSameAsPrevious(runEnd);
RemoveRunIfSameAsPrevious(runStart);
}
return true;
}

void RunStyles::SetValueAt(int position, int value) {
int len = 1;
FillRange(position, value, len);
}

void RunStyles::InsertSpace(int position, int insertLength) {
int runStart = RunFromPosition(position);
if (starts->PositionFromPartition(runStart) == position) {
int runStyle = ValueAt(position);
// Inserting at start of run so make previous longer
if (runStart == 0) {
// Inserting at start of document so ensure 0
if (runStyle) {
styles->SetValueAt(0, 0);
starts->InsertPartition(1, 0);
styles->InsertValue(1, 1, runStyle);
starts->InsertText(0, insertLength);
} else {
starts->InsertText(runStart, insertLength);
}
} else {
if (runStyle) {
starts->InsertText(runStart-1, insertLength);
} else {
// Insert at end of run so do not extend style
starts->InsertText(runStart, insertLength);
}
}
} else {
starts->InsertText(runStart, insertLength);
}
}

void RunStyles::DeleteAll() {
delete starts;
starts = NULL;
delete styles;
styles = NULL;
starts = new Partitioning(8);
styles = new SplitVector<int>();
styles->InsertValue(0, 2, 0);
}

void RunStyles::DeleteRange(int position, int deleteLength) {
int end = position + deleteLength;
int runStart = RunFromPosition(position);
int runEnd = RunFromPosition(end);
if (runStart == runEnd) {
// Deleting from inside one run
starts->InsertText(runStart, -deleteLength);
} else {
runStart = SplitRun(position);
runEnd = SplitRun(end);
starts->InsertText(runStart, -deleteLength);
// Remove each old run over the range
for (int run=runStart; run<runEnd; run++) {
RemoveRun(runStart);
}
RemoveRunIfEmpty(runStart);
RemoveRunIfSameAsPrevious(runStart);
}
}

Something went wrong with that request. Please try again.