2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
#include "typingsettings.h"
|
|
|
|
|
|
2023-07-17 13:54:37 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextCursor>
|
|
|
|
|
#include <QTextDocument>
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
static const char autoIndentKey[] = "AutoIndent";
|
|
|
|
|
static const char tabKeyBehaviorKey[] = "TabKeyBehavior";
|
|
|
|
|
static const char smartBackspaceBehaviorKey[] = "SmartBackspaceBehavior";
|
2021-02-04 10:59:05 +01:00
|
|
|
static const char preferSingleLineCommentsKey[] = "PreferSingleLineComments";
|
2023-09-12 14:40:59 +02:00
|
|
|
static const char preferAfterWhitespaceCommentsKey[] = "PreferAfterWhitespaceComments";
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2023-08-23 16:11:48 +02:00
|
|
|
using namespace Utils;
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
|
|
|
|
|
TypingSettings::TypingSettings():
|
|
|
|
|
m_autoIndent(true),
|
|
|
|
|
m_tabKeyBehavior(TabNeverIndents),
|
2023-10-13 12:40:21 +02:00
|
|
|
m_smartBackspaceBehavior(BackspaceUnindents),
|
2021-02-04 10:59:05 +01:00
|
|
|
m_preferSingleLineComments(false)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 16:53:06 +02:00
|
|
|
Store TypingSettings::toMap() const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2021-12-01 15:41:12 +02:00
|
|
|
return {
|
|
|
|
|
{autoIndentKey, m_autoIndent},
|
|
|
|
|
{tabKeyBehaviorKey, m_tabKeyBehavior},
|
|
|
|
|
{smartBackspaceBehaviorKey, m_smartBackspaceBehavior},
|
2023-09-12 14:40:59 +02:00
|
|
|
{preferSingleLineCommentsKey, m_preferSingleLineComments},
|
2023-09-13 14:44:07 +02:00
|
|
|
{preferAfterWhitespaceCommentsKey, m_commentPosition}
|
2021-12-01 15:41:12 +02:00
|
|
|
};
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-23 16:53:06 +02:00
|
|
|
void TypingSettings::fromMap(const Store &map)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2021-12-01 15:41:12 +02:00
|
|
|
m_autoIndent = map.value(autoIndentKey, m_autoIndent).toBool();
|
|
|
|
|
m_tabKeyBehavior = (TabKeyBehavior) map.value(tabKeyBehaviorKey, m_tabKeyBehavior).toInt();
|
|
|
|
|
m_smartBackspaceBehavior = (SmartBackspaceBehavior)map.value(
|
|
|
|
|
smartBackspaceBehaviorKey, m_smartBackspaceBehavior).toInt();
|
2021-02-04 10:59:05 +01:00
|
|
|
m_preferSingleLineComments =
|
2021-12-01 15:41:12 +02:00
|
|
|
map.value(preferSingleLineCommentsKey, m_preferSingleLineComments).toBool();
|
2023-09-13 14:44:07 +02:00
|
|
|
m_commentPosition = CommentPosition(
|
|
|
|
|
std::clamp(map.value(preferAfterWhitespaceCommentsKey, m_commentPosition).toInt(),
|
|
|
|
|
int(Automatic),
|
|
|
|
|
int(AfterWhitespace)));
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TypingSettings::equals(const TypingSettings &ts) const
|
|
|
|
|
{
|
|
|
|
|
return m_autoIndent == ts.m_autoIndent
|
2023-09-13 14:44:07 +02:00
|
|
|
&& m_tabKeyBehavior == ts.m_tabKeyBehavior
|
|
|
|
|
&& m_smartBackspaceBehavior == ts.m_smartBackspaceBehavior
|
|
|
|
|
&& m_preferSingleLineComments == ts.m_preferSingleLineComments
|
|
|
|
|
&& m_commentPosition == ts.m_commentPosition;
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-28 09:13:57 +02:00
|
|
|
bool TypingSettings::tabShouldIndent(const QTextDocument *document,
|
|
|
|
|
const QTextCursor &cursor,
|
|
|
|
|
int *suggestedPosition) const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
if (m_tabKeyBehavior == TabNeverIndents)
|
|
|
|
|
return false;
|
|
|
|
|
QTextCursor tc = cursor;
|
|
|
|
|
if (suggestedPosition)
|
|
|
|
|
*suggestedPosition = tc.position(); // At least suggest original position
|
|
|
|
|
tc.movePosition(QTextCursor::StartOfLine);
|
|
|
|
|
if (tc.atBlockEnd()) // cursor was on a blank line
|
|
|
|
|
return true;
|
|
|
|
|
if (document->characterAt(tc.position()).isSpace()) {
|
|
|
|
|
tc.movePosition(QTextCursor::WordRight);
|
|
|
|
|
if (tc.positionInBlock() >= cursor.positionInBlock()) {
|
|
|
|
|
if (suggestedPosition)
|
|
|
|
|
*suggestedPosition = tc.position(); // Suggest position after whitespace
|
|
|
|
|
if (m_tabKeyBehavior == TabLeadingWhitespaceIndents)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (m_tabKeyBehavior == TabAlwaysIndents);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace TextEditor
|