Debian Codegraph @

basic/source/classes/codecompletecache.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <basic/codecompletecache.hxx>
21 #include <iostream>
22 #include <officecfg/Office/BasicIDE.hxx>
23
24 namespace
25 {
26 CodeCompleteOptions& theCodeCompleteOptions()
27 {
28 static CodeCompleteOptions SINGLETON;
29 return SINGLETON;
30 }
31 }
32
33 CodeCompleteOptions::CodeCompleteOptions()
34 {
35 bIsAutoCorrectOn = officecfg::Office::BasicIDE::Autocomplete::AutoCorrect::get();
36 bIsAutoCloseParenthesisOn = officecfg::Office::BasicIDE::Autocomplete::AutocloseParenthesis::get();
37 bIsAutoCloseQuotesOn = officecfg::Office::BasicIDE::Autocomplete::AutocloseDoubleQuotes::get();
38 bIsProcedureAutoCompleteOn = officecfg::Office::BasicIDE::Autocomplete::AutocloseProc::get();
39 bIsCodeCompleteOn = officecfg::Office::BasicIDE::Autocomplete::CodeComplete::get();
40 bExtendedTypeDeclarationOn = officecfg::Office::BasicIDE::Autocomplete::UseExtended::get();
41 }
42
43 bool CodeCompleteOptions::IsCodeCompleteOn()
44 {
45 return theCodeCompleteOptions().bIsCodeCompleteOn;
46 }
47
48 void CodeCompleteOptions::SetCodeCompleteOn( bool b )
49 {
50 theCodeCompleteOptions().bIsCodeCompleteOn = b;
51 }
52
53 bool CodeCompleteOptions::IsExtendedTypeDeclaration()
54 {
55 return theCodeCompleteOptions().bExtendedTypeDeclarationOn;
56 }
57
58 void CodeCompleteOptions::SetExtendedTypeDeclaration( bool b )
59 {
60 theCodeCompleteOptions().bExtendedTypeDeclarationOn = b;
61 }
62
63 bool CodeCompleteOptions::IsProcedureAutoCompleteOn()
64 {
65 return theCodeCompleteOptions().bIsProcedureAutoCompleteOn;
66 }
67
68 void CodeCompleteOptions::SetProcedureAutoCompleteOn( bool b )
69 {
70 theCodeCompleteOptions().bIsProcedureAutoCompleteOn = b;
71 }
72
73 bool CodeCompleteOptions::IsAutoCloseQuotesOn()
74 {
75 return theCodeCompleteOptions().bIsAutoCloseQuotesOn;
76 }
77
78 void CodeCompleteOptions::SetAutoCloseQuotesOn( bool b )
79 {
80 theCodeCompleteOptions().bIsAutoCloseQuotesOn = b;
81 }
82
83 bool CodeCompleteOptions::IsAutoCloseParenthesisOn()
84 {
85 return theCodeCompleteOptions().bIsAutoCloseParenthesisOn;
86 }
87
88 void CodeCompleteOptions::SetAutoCloseParenthesisOn( bool b )
89 {
90 theCodeCompleteOptions().bIsAutoCloseParenthesisOn = b;
91 }
92
93 bool CodeCompleteOptions::IsAutoCorrectOn()
94 {
95 return theCodeCompleteOptions().bIsAutoCorrectOn;
96 }
97
98 void CodeCompleteOptions::SetAutoCorrectOn( bool b )
99 {
100 theCodeCompleteOptions().bIsAutoCorrectOn = b;
101 }
102
103 std::ostream& operator<< (std::ostream& aStream, const CodeCompleteDataCache& aCache)
104 {
105 aStream << "Global variables" << std::endl;
106 for (auto const& globalVar : aCache.aGlobalVars)
107 {
108 aStream << globalVar.first << "," << globalVar.second << std::endl;
109 }
110 aStream << "Local variables" << std::endl;
111 for (auto const& varScope : aCache.aVarScopes)
112 {
113 aStream << varScope.first << std::endl;
114 CodeCompleteVarTypes aVarTypes = varScope.second;
115 for (auto const& varType : aVarTypes)
116 {
117 aStream << "\t" << varType.first << "," << varType.second << std::endl;
118 }
119 }
120 aStream << "-----------------" << std::endl;
121 return aStream;
122 }
123
124 void CodeCompleteDataCache::Clear()
125 {
126 aVarScopes.clear();
127 aGlobalVars.clear();
128 }
129
130 void CodeCompleteDataCache::InsertGlobalVar( const OUString& sVarName, const OUString& sVarType )
131 {
132 aGlobalVars.emplace( sVarName, sVarType );
133 }
134
135 void CodeCompleteDataCache::InsertLocalVar( const OUString& sProcName, const OUString& sVarName, const OUString& sVarType )
136 {
137 if( !aVarScopes.contains(sProcName) ) //new procedure
138 {
139 CodeCompleteVarTypes aTypes;
140 aTypes.emplace( sVarName, sVarType );
141 aVarScopes.emplace( sProcName, aTypes );
142 }
143 else
144 {
145 CodeCompleteVarTypes aTypes = aVarScopes[ sProcName ];
146 aTypes.emplace( sVarName, sVarType );
147 aVarScopes[ sProcName ] = std::move(aTypes);
148 }
149 }
150
151 OUString CodeCompleteDataCache::GetVarType( std::u16string_view sVarName ) const
152 {
153 for (auto const& varScope : aVarScopes)
154 {
155 CodeCompleteVarTypes aTypes = varScope.second;
156 for (auto const& elem : aTypes)
157 {
158 if( elem.first.equalsIgnoreAsciiCase( sVarName ) )
159 {
160 return elem.second;
161 }
162 }
163 }
164 //not a local, search global scope
165 for (auto const& globalVar : aGlobalVars)
166 {
167 if( globalVar.first.equalsIgnoreAsciiCase( sVarName ) )
168 return globalVar.second;
169 }
170 return OUString(); //not found
171 }
172
173 OUString CodeCompleteDataCache::GetCorrectCaseVarName( std::u16string_view sVarName, std::u16string_view sActProcName ) const
174 {
175 for (auto const& varScope : aVarScopes)
176 {
177 CodeCompleteVarTypes aTypes = varScope.second;
178 for (auto const& elem : aTypes)
179 {
180 if( elem.first.equalsIgnoreAsciiCase( sVarName ) && varScope.first.equalsIgnoreAsciiCase( sActProcName ) )
181 {
182 return elem.first;
183 }
184 }
185 }
186 // search global scope
187 for (auto const& globalVar : aGlobalVars)
188 {
189 if( globalVar.first.equalsIgnoreAsciiCase( sVarName ) )
190 return globalVar.first;
191 }
192 return OUString(); //not found
193 }
194
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */