Changeset View
Changeset View
Standalone View
Standalone View
src/view/kateview.cpp
Show First 20 Lines • Show All 1188 Lines • ▼ Show 20 Line(s) | 1175 | { | |||
---|---|---|---|---|---|
1189 | /*a = ac->addAction(QLatin1String("folding_expandall")); | 1189 | /*a = ac->addAction(QLatin1String("folding_expandall")); | ||
1190 | a->setText(i18n("Unfold All Nodes")); | 1190 | a->setText(i18n("Unfold All Nodes")); | ||
1191 | connect(a, SIGNAL(triggered(bool)), doc()->foldingTree(), SLOT(expandAll())); | 1191 | connect(a, SIGNAL(triggered(bool)), doc()->foldingTree(), SLOT(expandAll())); | ||
1192 | 1192 | | |||
1193 | a = ac->addAction(QLatin1String("folding_collapse_dsComment")); | 1193 | a = ac->addAction(QLatin1String("folding_collapse_dsComment")); | ||
1194 | a->setText(i18n("Fold Multiline Comments")); | 1194 | a->setText(i18n("Fold Multiline Comments")); | ||
1195 | connect(a, SIGNAL(triggered(bool)), doc()->foldingTree(), SLOT(collapseAll_dsComments())); | 1195 | connect(a, SIGNAL(triggered(bool)), doc()->foldingTree(), SLOT(collapseAll_dsComments())); | ||
1196 | */ | 1196 | */ | ||
1197 | a = ac->addAction(QStringLiteral("folding_collapselocal")); | 1197 | a = ac->addAction(QStringLiteral("folding_toggle_current")); | ||
1198 | a->setText(i18n("Fold Current Node")); | 1198 | a->setText(i18n("Toggle Current Node")); | ||
1199 | connect(a, SIGNAL(triggered(bool)), SLOT(slotCollapseLocal())); | 1199 | connect(a, &QAction::triggered, this, &KTextEditor::ViewPrivate::slotToggleFolding); | ||
1200 | 1200 | | |||
1201 | a = ac->addAction(QStringLiteral("folding_expandlocal")); | 1201 | a = ac->addAction(QStringLiteral("folding_toggle_in_current")); | ||
1202 | a->setText(i18n("Unfold Current Node")); | 1202 | a->setText(i18n("Toggle Contained Nodes")); | ||
1203 | connect(a, SIGNAL(triggered(bool)), SLOT(slotExpandLocal())); | 1203 | connect(a, &QAction::triggered, this, &KTextEditor::ViewPrivate::slotToggleFoldingsInRange); | ||
1204 | } | 1204 | } | ||
1205 | 1205 | | |||
1206 | void KTextEditor::ViewPrivate::slotFoldToplevelNodes() | 1206 | void KTextEditor::ViewPrivate::slotFoldToplevelNodes() | ||
1207 | { | 1207 | { | ||
1208 | for (int line = 0; line < doc()->lines(); ++line) { | 1208 | for (int line = 0; line < doc()->lines(); ++line) { | ||
1209 | if (textFolding().isLineVisible(line)) { | 1209 | if (textFolding().isLineVisible(line)) { | ||
1210 | foldLine(line); | 1210 | foldLine(line); | ||
1211 | } | 1211 | } | ||
1212 | } | 1212 | } | ||
1213 | } | 1213 | } | ||
1214 | 1214 | | |||
1215 | void KTextEditor::ViewPrivate::slotExpandToplevelNodes() | 1215 | void KTextEditor::ViewPrivate::slotExpandToplevelNodes() | ||
1216 | { | 1216 | { | ||
1217 | const auto topLevelRanges(textFolding().foldingRangesForParentRange()); | 1217 | const auto topLevelRanges(textFolding().foldingRangesForParentRange()); | ||
1218 | for (const auto &range : topLevelRanges) { | 1218 | for (const auto &range : topLevelRanges) { | ||
1219 | textFolding().unfoldRange(range.first); | 1219 | textFolding().unfoldRange(range.first); | ||
1220 | } | 1220 | } | ||
1221 | } | 1221 | } | ||
1222 | 1222 | | |||
1223 | void KTextEditor::ViewPrivate::slotCollapseLocal() | 1223 | void KTextEditor::ViewPrivate::slotToggleFolding() | ||
1224 | { | 1224 | { | ||
1225 | foldLine(cursorPosition().line()); | 1225 | int line = cursorPosition().line(); | ||
1226 | bool actionDone = false; | ||||
1227 | while (!actionDone && (line > -1)) { | ||||
1228 | actionDone = unfoldLine(line); | ||||
1229 | if (!actionDone) { | ||||
1230 | actionDone = foldLine(line--).isValid(); | ||||
1231 | } | ||||
1232 | } | ||||
1226 | } | 1233 | } | ||
1227 | 1234 | | |||
1228 | void KTextEditor::ViewPrivate::slotExpandLocal() | 1235 | void KTextEditor::ViewPrivate::slotToggleFoldingsInRange() | ||
1229 | { | 1236 | { | ||
1230 | unfoldLine(cursorPosition().line()); | 1237 | int line = cursorPosition().line(); | ||
1238 | while (!toggleFoldingsInRange(line) && (line > -1)) { | ||||
1239 | --line; | ||||
1240 | } | ||||
1231 | } | 1241 | } | ||
1232 | 1242 | | |||
1233 | void KTextEditor::ViewPrivate::foldLine(int startLine) | 1243 | KTextEditor::Range KTextEditor::ViewPrivate::foldLine(int line) | ||
1234 | { | 1244 | { | ||
1235 | // only for valid lines | 1245 | KTextEditor::Range foldingRange = doc()->buffer().computeFoldingRangeForStartLine(line); | ||
1236 | if (startLine < 0 || startLine >= doc()->buffer().lines()) { | 1246 | if (!foldingRange.isValid()) { | ||
1237 | return; | 1247 | return foldingRange; | ||
1248 | } | ||||
1249 | | ||||
1250 | // Ensure not to fold the end marker to avoid a deceptive look, but only on token based folding | ||||
1251 | Kate::TextLine startTextLine = doc()->buffer().plainLine(line); | ||||
1252 | if (!startTextLine->markedAsFoldingStartIndentation()) { | ||||
1253 | const int adjustedLine = foldingRange.end().line() - 1; | ||||
1254 | foldingRange.setEnd(KTextEditor::Cursor(adjustedLine, doc()->buffer().plainLine(adjustedLine)->length())); | ||||
1255 | } | ||||
1256 | | ||||
1257 | // Don't try to fold a single line, which can happens due to adjustment above | ||||
1258 | // FIXME Avoid to offer such a folding marker | ||||
1259 | if (!foldingRange.onSingleLine()) { | ||||
1260 | textFolding().newFoldingRange(foldingRange, Kate::TextFolding::Folded); | ||||
1238 | } | 1261 | } | ||
1239 | 1262 | | |||
1240 | // try to fold all known ranges | 1263 | return foldingRange; | ||
1241 | QVector<QPair<qint64, Kate::TextFolding::FoldingRangeFlags> > startingRanges = textFolding().foldingRangesStartingOnLine(startLine); | 1264 | } | ||
1265 | | ||||
1266 | bool KTextEditor::ViewPrivate::unfoldLine(int line) | ||||
1267 | { | ||||
1268 | bool actionDone = false; | ||||
1269 | | ||||
1270 | // ask the folding info for this line, if any folds are around! | ||||
1271 | // auto = QVector<QPair<qint64, Kate::TextFolding::FoldingRangeFlags>> | ||||
1272 | auto startingRanges = textFolding().foldingRangesStartingOnLine(line); | ||||
1242 | for (int i = 0; i < startingRanges.size(); ++i) { | 1273 | for (int i = 0; i < startingRanges.size(); ++i) { | ||
1243 | textFolding().foldRange(startingRanges[i].first); | 1274 | actionDone |= textFolding().unfoldRange(startingRanges[i].first); | ||
1244 | } | 1275 | } | ||
1245 | 1276 | | |||
1246 | // try if the highlighting can help us and create a fold | 1277 | return actionDone; | ||
1247 | textFolding().newFoldingRange(doc()->buffer().computeFoldingRangeForStartLine(startLine), Kate::TextFolding::Folded); | | |||
1248 | } | 1278 | } | ||
1249 | 1279 | | |||
1250 | void KTextEditor::ViewPrivate::unfoldLine(int startLine) | 1280 | bool KTextEditor::ViewPrivate::toggleFoldingOfLine(int line) | ||
1251 | { | 1281 | { | ||
1252 | // only for valid lines | 1282 | bool actionDone = unfoldLine(line); | ||
1253 | if (startLine < 0 || startLine >= doc()->buffer().lines()) { | 1283 | if (!actionDone) { | ||
1254 | return; | 1284 | actionDone = foldLine(line).isValid(); | ||
1255 | } | 1285 | } | ||
1256 | 1286 | | |||
1257 | // try to unfold all known ranges | 1287 | return actionDone; | ||
1258 | QVector<QPair<qint64, Kate::TextFolding::FoldingRangeFlags> > startingRanges = textFolding().foldingRangesStartingOnLine(startLine); | 1288 | } | ||
1259 | for (int i = 0; i < startingRanges.size(); ++i) { | 1289 | | ||
1260 | textFolding().unfoldRange(startingRanges[i].first); | 1290 | bool KTextEditor::ViewPrivate::toggleFoldingsInRange(int line) | ||
1291 | { | ||||
1292 | KTextEditor::Range foldingRange = doc()->buffer().computeFoldingRangeForStartLine(line); | ||||
1293 | if (!foldingRange.isValid()) { | ||||
1294 | // Either line is not valid or there is no start range | ||||
1295 | return false; | ||||
1296 | } | ||||
1297 | | ||||
1298 | bool actionDone = false; // Track success | ||||
1299 | | ||||
1300 | // Don't be too eager but obliging! Only toggle containing ranges which are | ||||
1301 | // visible -> Be done when the range is folded | ||||
1302 | actionDone |= unfoldLine(line); | ||||
1303 | | ||||
1304 | if (!actionDone) { | ||||
1305 | // Unfold all in range, but not the range itself | ||||
1306 | for (int ln = foldingRange.start().line() + 1; ln < foldingRange.end().line(); ++ln) { | ||||
1307 | actionDone |= unfoldLine(ln); | ||||
1308 | } | ||||
1261 | } | 1309 | } | ||
1310 | | ||||
1311 | if (!actionDone) { | ||||
1312 | // Fold all in range, but not the range itself | ||||
1313 | for (int ln = foldingRange.start().line() + 1; ln < foldingRange.end().line(); ++ln) { | ||||
1314 | KTextEditor::Range fr = foldLine(ln); | ||||
1315 | if (fr.isValid()) { | ||||
1316 | ln = fr.end().line() - 1; | ||||
1317 | actionDone = true; | ||||
1318 | } | ||||
1319 | } | ||||
1320 | } | ||||
1321 | | ||||
1322 | if (!actionDone) { | ||||
1323 | // At this point was an unfolded range clicked which contains no "childs" | ||||
1324 | // We assume the user want to fold it by the wrong button, be obliging! | ||||
1325 | actionDone |= foldLine(line).isValid(); | ||||
1326 | } | ||||
1327 | | ||||
1328 | // At this point we should be always true | ||||
1329 | return actionDone; | ||||
1262 | } | 1330 | } | ||
1263 | 1331 | | |||
1264 | KTextEditor::View::ViewMode KTextEditor::ViewPrivate::viewMode() const | 1332 | KTextEditor::View::ViewMode KTextEditor::ViewPrivate::viewMode() const | ||
1265 | { | 1333 | { | ||
1266 | return currentInputMode()->viewMode(); | 1334 | return currentInputMode()->viewMode(); | ||
1267 | } | 1335 | } | ||
1268 | 1336 | | |||
1269 | QString KTextEditor::ViewPrivate::viewModeHuman() const | 1337 | QString KTextEditor::ViewPrivate::viewModeHuman() const | ||
▲ Show 20 Lines • Show All 712 Lines • ▼ Show 20 Line(s) | 2049 | } else { | |||
1982 | m_autoFoldedFirstLine = false; | 2050 | m_autoFoldedFirstLine = false; | ||
1983 | } | 2051 | } | ||
1984 | 2052 | | |||
1985 | #if 0 | 2053 | #if 0 | ||
1986 | // FIXME: FOLDING | 2054 | // FIXME: FOLDING | ||
1987 | const QStringList l = { | 2055 | const QStringList l = { | ||
1988 | QStringLiteral("folding_toplevel") | 2056 | QStringLiteral("folding_toplevel") | ||
1989 | , QStringLiteral("folding_expandtoplevel") | 2057 | , QStringLiteral("folding_expandtoplevel") | ||
1990 | , QStringLiteral("folding_collapselocal") | 2058 | , QStringLiteral("folding_toggle_current") | ||
1991 | , QStringLiteral("folding_expandlocal") | 2059 | , QStringLiteral("folding_toggle_in_current") | ||
1992 | }; | 2060 | }; | ||
1993 | 2061 | | |||
1994 | QAction *a = 0; | 2062 | QAction *a = 0; | ||
1995 | for (int z = 0; z < l.size(); z++) | 2063 | for (int z = 0; z < l.size(); z++) | ||
1996 | if ((a = actionCollection()->action(l[z].toAscii().constData()))) { | 2064 | if ((a = actionCollection()->action(l[z].toAscii().constData()))) { | ||
1997 | a->setEnabled(doc()->highlight() && doc()->highlight()->allowsFolding()); | 2065 | a->setEnabled(doc()->highlight() && doc()->highlight()->allowsFolding()); | ||
1998 | } | 2066 | } | ||
1999 | #endif | 2067 | #endif | ||
▲ Show 20 Lines • Show All 1874 Lines • Show Last 20 Lines |