7#include "editorconfig.h"
9#include "katedocument.h"
10#include "katepartdebug.h"
16static bool checkBoolValue(
QString val,
bool *result)
19 static const QStringList trueValues{QStringLiteral(
"1"), QStringLiteral(
"on"), QStringLiteral(
"true")};
20 if (trueValues.contains(val)) {
25 static const QStringList falseValues{QStringLiteral(
"0"), QStringLiteral(
"off"), QStringLiteral(
"false")};
26 if (falseValues.contains(val)) {
37static bool checkIntValue(
const QString &val,
int *result)
40 *result = val.
toInt(&ret);
45 : m_document(document)
46 , m_handle(editorconfig_handle_init())
50EditorConfig::~EditorConfig()
52 editorconfig_handle_destroy(m_handle);
55int EditorConfig::parse()
60 if (code == EDITORCONFIG_PARSE_MEMORY_ERROR) {
61 qCDebug(LOG_KTE) <<
"Failed to parse .editorconfig, memory error occurred";
62 }
else if (code > 0) {
63 qCDebug(LOG_KTE) <<
"Failed to parse .editorconfig, error in line" << code;
65 qCDebug(LOG_KTE) <<
"Failed to parse .editorconfig, unknown error";
72 const unsigned int count = editorconfig_handle_get_name_value_count(m_handle);
75 bool setIndentSizeAsTabWidth =
false;
78 bool indentSizeSet =
false;
79 bool tabWidthSet =
false;
82 int tabWidth = m_document->
config()->tabWidth();
84 for (
unsigned int i = 0; i < count; ++i) {
86 const char *rawKey =
nullptr;
87 const char *rawValue =
nullptr;
94 editorconfig_handle_get_name_value(m_handle, i, &rawKey, &rawValue);
106 eols << QStringLiteral(
"lf") << QStringLiteral(
"crlf") << QStringLiteral(
"cr");
108 if ((intValue = eols.
indexOf(value)) != -1) {
109 m_document->
config()->setEol(intValue);
110 m_document->
config()->setAllowEolDetection(
false);
112 qCDebug(LOG_KTE) <<
"End of line in .editorconfig other than unix/dos/mac";
116 setIndentSizeAsTabWidth =
true;
117 }
else if (checkIntValue(value, &intValue)) {
118 m_document->
config()->setIndentationWidth(intValue);
119 indentSizeSet =
true;
121 qCDebug(LOG_KTE) <<
"Indent size in .editorconfig not a number, nor tab";
125 m_document->
config()->setReplaceTabsDyn(
false);
127 m_document->
config()->setReplaceTabsDyn(
true);
129 qCDebug(LOG_KTE) <<
"Indent style in .editorconfig other than tab or space";
131 }
else if (
QLatin1String(
"insert_final_newline") == key && checkBoolValue(value, &boolValue)) {
132 m_document->
config()->setNewLineAtEof(boolValue);
133 }
else if (
QLatin1String(
"max_line_length") == key && checkIntValue(value, &intValue)) {
134 m_document->
config()->setWordWrapAt(intValue);
135 }
else if (
QLatin1String(
"tab_width") == key && checkIntValue(value, &intValue)) {
136 m_document->
config()->setTabWidth(intValue);
139 }
else if (
QLatin1String(
"trim_trailing_whitespace") == key && checkBoolValue(value, &boolValue)) {
141 m_document->
config()->setRemoveSpaces(1 );
143 m_document->
config()->setRemoveSpaces(0);
148 if (setIndentSizeAsTabWidth) {
149 m_document->
config()->setIndentationWidth(tabWidth);
150 }
else if (!tabWidthSet && indentSizeSet) {
151 m_document->
config()->setTabWidth(m_document->
config()->indentationWidth());
Backend of KTextEditor::Document related public KTextEditor interfaces.
bool setEncoding(const QString &e) override
Set the encoding for this document.
KateDocumentConfig * config()
Configuration.
int toInt(bool *ok, int base) const const
QString toLower() const const
std::string toStdString() const const
QString trimmed() const const
qsizetype indexOf(const QRegularExpression &re, qsizetype from) const const
QString toLocalFile() const const