1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "unsavedfile.h"
27
28 #include "clangfilepath.h"
29 #include "utf8positionfromlinecolumn.h"
30
31 #include <ostream>
32
33 #include <utils/qtcassert.h>
34
35 namespace ClangBackEnd {
36
UnsavedFile()37 UnsavedFile::UnsavedFile()
38 {
39 }
40
UnsavedFile(const Utf8String & filePath,const Utf8String & fileContent)41 UnsavedFile::UnsavedFile(const Utf8String &filePath,
42 const Utf8String &fileContent)
43 : m_filePath(filePath)
44 , m_nativeFilePath(FilePath::toNativeSeparators(filePath))
45 , m_fileContent(fileContent)
46 {
47 }
48
nativeFilePath() const49 Utf8String UnsavedFile::nativeFilePath() const
50 {
51 return m_nativeFilePath;
52 }
53
filePath() const54 Utf8String UnsavedFile::filePath() const
55 {
56 return m_filePath;
57 }
58
fileContent() const59 Utf8String UnsavedFile::fileContent() const
60 {
61 return m_fileContent;
62 }
63
toUtf8Position(int line,int column,bool * ok) const64 uint UnsavedFile::toUtf8Position(int line, int column, bool *ok) const
65 {
66 Utf8PositionFromLineColumn converter(m_fileContent.constData());
67 if (converter.find(line, column)) {
68 *ok = true;
69 return converter.position();
70 }
71
72 *ok = false;
73 return 0;
74 }
75
hasCharacterAt(int line,int column,char character) const76 bool UnsavedFile::hasCharacterAt(int line, int column, char character) const
77 {
78 bool positionIsOk = false;
79 const uint utf8Position = toUtf8Position(line, column, &positionIsOk);
80
81 return positionIsOk && hasCharacterAt(utf8Position, character);
82 }
83
lineRange(int fromLine,int toLine) const84 Utf8String UnsavedFile::lineRange(int fromLine, int toLine) const
85 {
86 if (fromLine > toLine)
87 return Utf8String();
88
89 // Find start of first line
90 bool ok = false;
91 const uint fromPosition = toUtf8Position(fromLine, 1, &ok);
92 QTC_ASSERT(ok, return Utf8String());
93
94 // Find end of last line
95 uint toPosition = toUtf8Position(toLine, 1, &ok);
96 QTC_ASSERT(ok, return Utf8String());
97 const uint endPosition = uint(m_fileContent.byteSize());
98 while (toPosition < endPosition && m_fileContent.constData()[toPosition] != '\n')
99 ++toPosition;
100
101 return m_fileContent.mid(int(fromPosition), int(toPosition - fromPosition));
102 }
103
hasCharacterAt(uint position,char character) const104 bool UnsavedFile::hasCharacterAt(uint position, char character) const
105 {
106 if (position < uint(m_fileContent.byteSize()))
107 return m_fileContent.constData()[position] == character;
108
109 return false;
110 }
111
replaceAt(uint position,uint length,const Utf8String & replacement)112 bool UnsavedFile::replaceAt(uint position, uint length, const Utf8String &replacement)
113 {
114 if (position < uint(m_fileContent.byteSize())) {
115 m_fileContent.replace(int(position), int(length), replacement);
116 return true;
117 }
118
119 return false;
120 }
121
operator <<(std::ostream & os,const UnsavedFile & unsavedFile)122 std::ostream &operator<<(std::ostream &os, const UnsavedFile &unsavedFile)
123 {
124 os << "UnsavedFile("
125 << unsavedFile.m_filePath << ", "
126 << unsavedFile.m_fileContent << ", "
127 << unsavedFile.m_fileContent << ")";
128
129 return os;
130 }
131
132 } // namespace ClangBackEnd
133