|
| 1 | +import { Autowired, Injectable } from '@ali/common-di'; |
| 2 | +import { RuntimeConfig } from '@alipay/alex-core'; |
| 3 | + |
| 4 | +import { CodeModelService } from './code-model.service'; |
| 5 | + |
| 6 | +/** |
| 7 | + * 调整 runtimeConfig |
| 8 | + */ |
| 9 | +@Injectable() |
| 10 | +export class Configure { |
| 11 | + @Autowired(RuntimeConfig) |
| 12 | + runtimeConfig: RuntimeConfig; |
| 13 | + |
| 14 | + @Autowired() |
| 15 | + codeModel: CodeModelService; |
| 16 | + |
| 17 | + configTextSearch() { |
| 18 | + if (this.runtimeConfig.textSearch) return; |
| 19 | + |
| 20 | + this.runtimeConfig.textSearch = { |
| 21 | + config: { |
| 22 | + regexp: false, |
| 23 | + replace: false, |
| 24 | + caseSensitive: 'local', |
| 25 | + wordMatch: 'local', |
| 26 | + include: 'local', |
| 27 | + exclude: 'local', |
| 28 | + }, |
| 29 | + provideResults: async (query, options, progress) => { |
| 30 | + const requestResults = await this.codeModel.rootRepository.request.searchContent( |
| 31 | + query.pattern, |
| 32 | + { |
| 33 | + limit: options.maxResults, |
| 34 | + } |
| 35 | + ); |
| 36 | + if (!requestResults.length) return; |
| 37 | + const searchString = query.pattern.toLowerCase(); |
| 38 | + const searchStringLen = searchString.length; |
| 39 | + requestResults.forEach(({ path, line, content }) => { |
| 40 | + const text = content.toLowerCase(); |
| 41 | + |
| 42 | + let lastMatchIndex = -searchStringLen; |
| 43 | + const matches: [number, number][] = []; |
| 44 | + while ( |
| 45 | + (lastMatchIndex = text.indexOf(searchString, lastMatchIndex + searchStringLen)) !== -1 |
| 46 | + ) { |
| 47 | + matches.push([lastMatchIndex, lastMatchIndex + searchStringLen]); |
| 48 | + } |
| 49 | + if (matches.length) { |
| 50 | + progress.report({ |
| 51 | + path, |
| 52 | + lineNumber: line, |
| 53 | + preview: { |
| 54 | + text: content, |
| 55 | + matches, |
| 56 | + }, |
| 57 | + }); |
| 58 | + } |
| 59 | + }); |
| 60 | + }, |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + configFileSearch() { |
| 65 | + if (this.runtimeConfig.fileSearch) return; |
| 66 | + |
| 67 | + this.runtimeConfig.fileSearch = { |
| 68 | + config: { |
| 69 | + include: 'local', |
| 70 | + exclude: 'local', |
| 71 | + }, |
| 72 | + provideResults: (query, options) => { |
| 73 | + return this.codeModel.rootRepository.request.searchFile(query.pattern, { |
| 74 | + limit: options.maxResults, |
| 75 | + }); |
| 76 | + }, |
| 77 | + }; |
| 78 | + } |
| 79 | +} |
0 commit comments