Changeset View
Changeset View
Standalone View
Standalone View
src/document/katedocument.cpp
Show First 20 Lines • Show All 91 Lines • ▼ Show 20 Line(s) | |||||
109 | } | 109 | } | ||
110 | 110 | | |||
111 | template<class C, class E> | 111 | template<class C, class E> | ||
112 | static bool contains(const std::initializer_list<C> & list, const E& entry) | 112 | static bool contains(const std::initializer_list<C> & list, const E& entry) | ||
113 | { | 113 | { | ||
114 | return indexOf(list, entry) >= 0; | 114 | return indexOf(list, entry) >= 0; | ||
115 | } | 115 | } | ||
116 | 116 | | |||
117 | static inline QChar matchingStartBracket(QChar c, bool withQuotes) | 117 | static inline QChar matchingStartBracket(const QChar &c) | ||
sars: I'm not familiar with the auto-bracket code, but the change from QChar to const QChar & does… | |||||
118 | { | 118 | { | ||
119 | switch (c.toLatin1()) { | 119 | switch (c.toLatin1()) { | ||
120 | case '}': return QLatin1Char('{'); | 120 | case '}': return QLatin1Char('{'); | ||
121 | case ']': return QLatin1Char('['); | 121 | case ']': return QLatin1Char('['); | ||
122 | case ')': return QLatin1Char('('); | 122 | case ')': return QLatin1Char('('); | ||
123 | case '\'': return withQuotes ? QLatin1Char('\'') : QChar(); | | |||
124 | case '"': return withQuotes ? QLatin1Char('"') : QChar(); | | |||
125 | } | 123 | } | ||
126 | return QChar(); | 124 | return QChar(); | ||
127 | } | 125 | } | ||
128 | 126 | | |||
129 | static inline QChar matchingEndBracket(QChar c, bool withQuotes) | 127 | static inline QChar matchingEndBracket(const QChar &c, bool withQuotes = true) | ||
130 | { | 128 | { | ||
131 | switch (c.toLatin1()) { | 129 | switch (c.toLatin1()) { | ||
132 | case '{': return QLatin1Char('}'); | 130 | case '{': return QLatin1Char('}'); | ||
133 | case '[': return QLatin1Char(']'); | 131 | case '[': return QLatin1Char(']'); | ||
134 | case '(': return QLatin1Char(')'); | 132 | case '(': return QLatin1Char(')'); | ||
135 | case '\'': return withQuotes ? QLatin1Char('\'') : QChar(); | 133 | case '\'': return withQuotes ? QLatin1Char('\'') : QChar(); | ||
136 | case '"': return withQuotes ? QLatin1Char('"') : QChar(); | 134 | case '"': return withQuotes ? QLatin1Char('"') : QChar(); | ||
137 | } | 135 | } | ||
138 | return QChar(); | 136 | return QChar(); | ||
139 | } | 137 | } | ||
140 | 138 | | |||
141 | static inline QChar matchingBracket(QChar c, bool withQuotes) | 139 | static inline QChar matchingBracket(const QChar &c) | ||
142 | { | 140 | { | ||
143 | QChar bracket = matchingStartBracket(c, withQuotes); | 141 | QChar bracket = matchingStartBracket(c); | ||
144 | if (bracket.isNull()) { | 142 | if (bracket.isNull()) { | ||
145 | bracket = matchingEndBracket(c, false); | 143 | bracket = matchingEndBracket(c, /*withQuotes=*/false); | ||
146 | } | 144 | } | ||
147 | return bracket; | 145 | return bracket; | ||
148 | } | 146 | } | ||
149 | 147 | | |||
150 | static inline bool isStartBracket(const QChar &c) | 148 | static inline bool isStartBracket(const QChar &c) | ||
151 | { | 149 | { | ||
152 | return ! matchingEndBracket(c, false).isNull(); | 150 | return ! matchingEndBracket(c, /*withQuotes=*/false).isNull(); | ||
153 | } | 151 | } | ||
154 | 152 | | |||
155 | static inline bool isEndBracket(const QChar &c) | 153 | static inline bool isEndBracket(const QChar &c) | ||
156 | { | 154 | { | ||
157 | return ! matchingStartBracket(c, false).isNull(); | 155 | return ! matchingStartBracket(c).isNull(); | ||
158 | } | 156 | } | ||
159 | 157 | | |||
160 | static inline bool isBracket(const QChar &c) | 158 | static inline bool isBracket(const QChar &c) | ||
161 | { | 159 | { | ||
162 | return isStartBracket(c) || isEndBracket(c); | 160 | return isStartBracket(c) || isEndBracket(c); | ||
163 | } | 161 | } | ||
164 | 162 | | |||
165 | /** | 163 | /** | ||
▲ Show 20 Lines • Show All 182 Lines • ▼ Show 20 Line(s) | 2933 | { | |||
2952 | } | 2950 | } | ||
2953 | 2951 | | |||
2954 | /** | 2952 | /** | ||
2955 | * auto bracket handling for newly inserted text | 2953 | * auto bracket handling for newly inserted text | ||
2956 | * remember if we should auto add some | 2954 | * remember if we should auto add some | ||
2957 | */ | 2955 | */ | ||
2958 | QChar closingBracket; | 2956 | QChar closingBracket; | ||
2959 | if (view->config()->autoBrackets() && chars.size() == 1) { | 2957 | if (view->config()->autoBrackets() && chars.size() == 1) { | ||
2958 | const QChar typedChar = chars.at(0); | ||||
2960 | /** | 2959 | /** | ||
2961 | * we inserted a bracket? | 2960 | * we inserted a bracket? | ||
2962 | * => remember the matching closing one | 2961 | * => remember the matching closing one | ||
2963 | */ | 2962 | */ | ||
2964 | closingBracket = matchingEndBracket(chars[0], true); | 2963 | closingBracket = matchingEndBracket(typedChar); | ||
2965 | 2964 | | |||
2966 | /** | 2965 | /** | ||
2967 | * closing bracket for the autobracket we inserted earlier? | 2966 | * closing bracket for the autobracket we inserted earlier? | ||
2968 | */ | 2967 | */ | ||
2969 | if ( m_currentAutobraceClosingChar == chars[0] && m_currentAutobraceRange ) { | 2968 | if ( m_currentAutobraceClosingChar == typedChar && m_currentAutobraceRange ) { | ||
2970 | // do nothing | 2969 | // do nothing | ||
2971 | m_currentAutobraceRange.reset(nullptr); | 2970 | m_currentAutobraceRange.reset(nullptr); | ||
2972 | view->cursorRight(); | 2971 | view->cursorRight(); | ||
2973 | return true; | 2972 | return true; | ||
2974 | } | 2973 | } | ||
2974 | | ||||
2975 | // Treat some char also as "auto bracket" only when we have a selection | ||||
2976 | // Some of these may handy when write Markdown, some perhaps elsewhere | ||||
2977 | if (view->selection() && closingBracket.isNull()) { | ||||
2978 | if (QStringLiteral("´`_.:|#@~*!?$%/\\=").contains(typedChar)) { | ||||
2979 | closingBracket = typedChar; | ||||
2980 | } else { | ||||
2981 | switch (typedChar.toLatin1()) { | ||||
2982 | case '>': closingBracket = QLatin1Char('<'); break; | ||||
2983 | case '<': closingBracket = QLatin1Char('>'); break; | ||||
2984 | } | ||||
2985 | } | ||||
2986 | } | ||||
2975 | } | 2987 | } | ||
2976 | 2988 | | |||
2977 | /** | 2989 | /** | ||
2978 | * selection around => special handling if we want to add auto brackets | 2990 | * selection around => special handling if we want to add auto brackets | ||
2979 | */ | 2991 | */ | ||
2980 | if (view->selection() && !closingBracket.isNull()) { | 2992 | if (view->selection() && !closingBracket.isNull()) { | ||
2981 | /** | 2993 | /** | ||
2982 | * add bracket at start + end of the selection | 2994 | * add bracket at start + end of the selection | ||
▲ Show 20 Lines • Show All 182 Lines • ▼ Show 20 Line(s) | 4071 | } else if (isBracket(right)) { | |||
4060 | bracket = right; | 4072 | bracket = right; | ||
4061 | } else if (isBracket(left)) { | 4073 | } else if (isBracket(left)) { | ||
4062 | range.setStart(KTextEditor::Cursor(range.start().line(), range.start().column() - 1)); | 4074 | range.setStart(KTextEditor::Cursor(range.start().line(), range.start().column() - 1)); | ||
4063 | bracket = left; | 4075 | bracket = left; | ||
4064 | } else { | 4076 | } else { | ||
4065 | return KTextEditor::Range::invalid(); | 4077 | return KTextEditor::Range::invalid(); | ||
4066 | } | 4078 | } | ||
4067 | 4079 | | |||
4068 | const QChar opposite = matchingBracket(bracket, false); | 4080 | const QChar opposite = matchingBracket(bracket); | ||
4069 | if (opposite.isNull()) { | 4081 | if (opposite.isNull()) { | ||
4070 | return KTextEditor::Range::invalid(); | 4082 | return KTextEditor::Range::invalid(); | ||
4071 | } | 4083 | } | ||
4072 | 4084 | | |||
4073 | const int searchDir = isStartBracket(bracket) ? 1 : -1; | 4085 | const int searchDir = isStartBracket(bracket) ? 1 : -1; | ||
4074 | uint nesting = 0; | 4086 | uint nesting = 0; | ||
4075 | 4087 | | |||
4076 | const int minLine = qMax(range.start().line() - maxLines, 0); | 4088 | const int minLine = qMax(range.start().line() - maxLines, 0); | ||
▲ Show 20 Lines • Show All 91 Lines • Show Last 20 Lines |
I'm not familiar with the auto-bracket code, but the change from QChar to const QChar & does not improve the amount of copied data.
Qt documentation: "Most compilers treat it like an unsigned short."
This means that in stead of adding unsigned short to the stack you add the 64bit reference and add the tiny overhead that the reference brings with it.
It probably has no practical difference tho....