@@ -3,7 +3,9 @@ import { ExtensionContext } from 'vscode';
33import { FlinkSQLLexer } from './FlinkSQLLexer' ; // 导入生成的词法分析器
44import { FlinkSQLParser } from './FlinkSQLParser' ; // 导入生成的解析器
55import { ANTLRInputStream , CommonTokenStream } from 'antlr4ts' ;
6- import { MyFlinkSqlVisitor } from './MyGrammar' ; // 导入生成的访问器
6+ import { MyFlinkSQLVisitor } from './Grammar' ;
7+ import { FlinkSQLReferenceProvider } from './Reference' ;
8+ import { FlinkSQLRenameProvider } from './Rename' ;
79import { ParserErrorListener , RecognitionException , Recognizer } from 'antlr4ts' ;
810const sqlFormatter = require ( 'sql-formatter-plus' ) ;
911
@@ -34,7 +36,7 @@ export function activate(context: ExtensionContext) {
3436
3537 context . subscriptions . push ( vscode . languages . registerReferenceProvider (
3638 [ { pattern : '**/*.sql' } , { pattern : '**/*.fql' } ] ,
37- new FqlReferenceProvider ( )
39+ new FlinkSQLReferenceProvider ( )
3840 ) ) ;
3941
4042 context . subscriptions . push ( vscode . commands . registerCommand ( 'extension.showReferences' , ( uri : vscode . Uri , position : vscode . Position , locations : vscode . Location [ ] ) => {
@@ -59,7 +61,7 @@ export function activate(context: ExtensionContext) {
5961 vscode . TextEdit . replace ( range , format ( document . getText ( range ) ) ) ,
6062 ] ,
6163 } ) ;
62- context . subscriptions . push ( vscode . languages . registerRenameProvider ( selector , new FqlRenameProvider ( ) ) ) ;
64+ context . subscriptions . push ( vscode . languages . registerRenameProvider ( selector , new FlinkSQLRenameProvider ( ) ) ) ;
6365
6466 // 注册插件的其他命令和功能...
6567}
@@ -95,79 +97,12 @@ function updateFeatureStatus() {
9597 const parseTree = parser . program ( ) ;
9698
9799 // 创建访问器实例并访问语法树,以获取语法错误和警告
98- const visitor = new MyFlinkSqlVisitor ( ) ;
100+ const visitor = new MyFlinkSQLVisitor ( ) ;
99101 visitor . visit ( parseTree ) ;
100102 const errors = visitor . getErrors ( ) ;
101103 errors . forEach ( error => {
102104 vscode . window . showInformationMessage ( "Visitor flink sql error. error: " + error ) ;
103105 } )
104-
105- // 使用VSCode的诊断API报告语法错误和警告
106- // const diagnostics: vscode.Diagnostic[] = errors.map(error => {
107- // const range = new vscode.Range(
108- // event.document.positionAt(error.getStartIndex()),
109- // event.document.positionAt(error.getStopIndex() + 1)
110- // );
111- // const diagnostic = new vscode.Diagnostic(range, error.message, vscode.DiagnosticSeverity.Error);
112- // return diagnostic;
113- // });
114-
115- // diagnosticCollection.set(event.document.uri, diagnostics);
116-
117106 } ) ;
118107 }
119- }
120-
121-
122- class FqlRenameProvider implements vscode . RenameProvider {
123- provideRenameEdits ( document : vscode . TextDocument , position : vscode . Position , newName : string , token : vscode . CancellationToken ) : vscode . ProviderResult < vscode . WorkspaceEdit > {
124- const wordRange = document . getWordRangeAtPosition ( position ) ;
125- const originalWord = document . getText ( wordRange ) ;
126-
127- const edit = new vscode . WorkspaceEdit ( ) ;
128- for ( let i = 0 ; i < document . lineCount ; i ++ ) {
129- const line = document . lineAt ( i ) ;
130- const start = line . text . indexOf ( originalWord ) ;
131- if ( start >= 0 ) {
132- // 检查单词前后是否有其他字符
133- const isWordBoundary = ( index : number ) => {
134- return index < 0 || index >= line . text . length || / \W / . test ( line . text [ index ] ) ;
135- } ;
136- if ( isWordBoundary ( start - 1 ) && isWordBoundary ( start + originalWord . length ) ) {
137- edit . replace ( document . uri , new vscode . Range ( new vscode . Position ( i , start ) , new vscode . Position ( i , start + originalWord . length ) ) , newName ) ;
138- }
139- }
140- }
141-
142- return edit ;
143- }
144- }
145-
146-
147- class FqlReferenceProvider implements vscode . ReferenceProvider {
148- provideReferences ( document : vscode . TextDocument , position : vscode . Position , options : { includeDeclaration : boolean } , token : vscode . CancellationToken ) : vscode . ProviderResult < vscode . Location [ ] > {
149- const wordRange = document . getWordRangeAtPosition ( position ) ;
150- if ( ! wordRange ) {
151- return [ ] ;
152- }
153- const word = document . getText ( wordRange ) ;
154-
155- const references : vscode . Location [ ] = [ ] ;
156-
157- for ( let line = 0 ; line < document . lineCount ; line ++ ) {
158- const lineOfCode = document . lineAt ( line ) ;
159- const index = lineOfCode . text . indexOf ( word ) ;
160-
161- if ( index >= 0 ) {
162- const referencePosition = new vscode . Position ( line , index ) ;
163- const referenceRange = document . getWordRangeAtPosition ( referencePosition ) ;
164- if ( referenceRange ) {
165- const referenceLocation = new vscode . Location ( document . uri , referenceRange ) ;
166- references . push ( referenceLocation ) ;
167- }
168- }
169- }
170-
171- return references ;
172- }
173108}
0 commit comments