@@ -51,9 +51,7 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
5151
5252 vscode::DocumentOnTypeFormattingOptions typeOptions;
5353
54- typeOptions.firstTriggerCharacter = " ;" ;
55-
56- typeOptions.moreTriggerCharacter = {" \" " , " \' " , " ," , " :" , " ." };
54+ typeOptions.firstTriggerCharacter = " \n " ;
5755
5856 result->capabilities .documentOnTypeFormattingProvider = typeOptions;
5957
@@ -75,7 +73,10 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
7573{
7674 for (auto & content : param->contentChanges )
7775 {
78- LanguageClient::GetInstance ().CacheFile (param->textDocument .uri , content.text );
76+ auto parser = LuaParser::LoadFromBuffer (std::move (content.text ));
77+ parser->BuildAstWithComment ();
78+
79+ LanguageClient::GetInstance ().CacheFile (param->textDocument .uri , parser);
7980
8081 LanguageClient::GetInstance ().DiagnosticFile (param->textDocument .uri );
8182 }
@@ -85,7 +86,9 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
8586std::shared_ptr<vscode::Serializable> LanguageService::OnDidOpen (
8687 std::shared_ptr<vscode::DidOpenTextDocumentParams> param)
8788{
88- LanguageClient::GetInstance ().CacheFile (param->textDocument .uri , param->textDocument .text );
89+ auto parser = LuaParser::LoadFromBuffer (std::move (param->textDocument .text ));
90+ parser->BuildAstWithComment ();
91+ LanguageClient::GetInstance ().CacheFile (param->textDocument .uri , parser);
8992 LanguageClient::GetInstance ().DiagnosticFile (param->textDocument .uri );
9093
9194 return nullptr ;
@@ -94,23 +97,20 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidOpen(
9497std::shared_ptr<vscode::Serializable> LanguageService::OnFormatting (
9598 std::shared_ptr<vscode::DocumentFormattingParams> param)
9699{
97- auto text = LanguageClient::GetInstance ().GetFile (param->textDocument .uri );
100+ auto parser = LanguageClient::GetInstance ().GetFileParser (param->textDocument .uri );
98101
99- auto lastOffset = text. size ();
102+ auto totalLine = parser-> GetTotalLine ();
100103
101104 auto result = std::make_shared<vscode::DocumentFormattingResult>();
102105
103- if (text. empty () )
106+ if (totalLine == 0 )
104107 {
105108 result->hasError = true ;
106109 return result;
107110 }
108111
109112 auto options = LanguageClient::GetInstance ().GetOptions (param->textDocument .uri );
110113
111- std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer (std::move (text));
112- parser->BuildAstWithComment ();
113-
114114 if (parser->HasError ())
115115 {
116116 result->hasError = true ;
@@ -124,7 +124,7 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnFormatting(
124124 edit.newText = formatter.GetFormattedText ();
125125 edit.range = vscode::Range (
126126 vscode::Position (0 , 0 ),
127- vscode::Position (parser-> GetLine (lastOffset), parser-> GetColumn (lastOffset) )
127+ vscode::Position (totalLine + 1 , 0 )
128128 );
129129 return result;
130130}
@@ -161,21 +161,12 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnEditorConfigUpdate(
161161std::shared_ptr<vscode::Serializable> LanguageService::OnRangeFormatting (
162162 std::shared_ptr<vscode::DocumentRangeFormattingParams> param)
163163{
164- auto text = LanguageClient::GetInstance ().GetFile (param->textDocument .uri );
164+ auto parser = LanguageClient::GetInstance ().GetFileParser (param->textDocument .uri );
165165
166166 auto result = std::make_shared<vscode::DocumentFormattingResult>();
167167
168- if (text.empty ())
169- {
170- result->hasError = true ;
171- return result;
172- }
173-
174168 auto options = LanguageClient::GetInstance ().GetOptions (param->textDocument .uri );
175169
176- std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer (std::move (text));
177- parser->BuildAstWithComment ();
178-
179170 if (parser->HasError ())
180171 {
181172 result->hasError = true ;
@@ -199,33 +190,29 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnRangeFormatting(
199190std::shared_ptr<vscode::Serializable> LanguageService::OnTypeFormatting (
200191 std::shared_ptr<vscode::TextDocumentPositionParams> param)
201192{
202- auto text = LanguageClient::GetInstance ().GetFile (param->textDocument .uri );
193+ auto parser = LanguageClient::GetInstance ().GetFileParser (param->textDocument .uri );
194+ auto position = param->position ;
203195
204196 auto result = std::make_shared<vscode::DocumentFormattingResult>();
205-
206- if (text.empty ())
197+ if (parser->IsEmptyLine (position.line - 1 ))
207198 {
208199 result->hasError = true ;
209200 return result;
210201 }
211202
212203 auto options = LanguageClient::GetInstance ().GetOptions (param->textDocument .uri );
213204
214- std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer (std::move (text));
215- parser->BuildAstWithComment ();
216-
217- // ºöÂÔ¼üÈë´íÎó
218- // if (parser->HasError())
219- // {
220- // result->hasError = true;
221- // return result;
222- // }
205+ if (parser->HasError ())
206+ {
207+ result->hasError = true ;
208+ return result;
209+ }
223210
224211 LuaFormatter formatter (parser, *options);
225212 formatter.BuildFormattedElement ();
226213
227214 auto & edit = result->edits .emplace_back ();
228- LuaFormatRange formattedRange (param-> position .line , param-> position .line );
215+ LuaFormatRange formattedRange (position.line - 1 , position.line - 1 );
229216
230217 edit.newText = formatter.GetRangeFormattedText (formattedRange);
231218 edit.range = vscode::Range (
0 commit comments