Skip to content

Commit

Permalink
fixup! Extract AtSuggestionCompleter
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Sep 10, 2023
1 parent f2423ca commit 9f1c203
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 45 deletions.
44 changes: 44 additions & 0 deletions src/providers/atsuggestion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as vscode from 'vscode'

import {AtSuggestion} from './atsuggestionlib/atsuggestion'
import {escapeRegExp} from '../utils/utils'

export class AtSuggestionCompleter implements vscode.CompletionItemProvider {
private readonly atSuggestion: AtSuggestion
private readonly triggerCharacter: string

constructor(
extension: {
readonly extensionRoot: string
},
triggerCharacter: string
) {
this.atSuggestion = new AtSuggestion(extension, triggerCharacter)
this.triggerCharacter = triggerCharacter
}

get readyPromise() {
return this.atSuggestion.readyPromise
}

provideCompletionItems(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken,
context: vscode.CompletionContext
): vscode.CompletionItem[] | undefined {
const line = document.lineAt(position).text.substring(0, position.character)
return this.completion(line, {document, position, token, context})
}

private completion(line: string, args: {document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext}): vscode.CompletionItem[] {
const escapedTriggerCharacter = escapeRegExp(this.triggerCharacter)
const reg = new RegExp(escapedTriggerCharacter + '[^\\\\s]*$')
const result = line.match(reg)
let suggestions: vscode.CompletionItem[] = []
if (result) {
suggestions = this.atSuggestion.provideFrom(result, args)
}
return suggestions
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as vscode from 'vscode'

import type {IProvider} from './interface'
import type {IProvider} from '../completer/interface'
import {escapeRegExp} from '../../utils/utils'
import { readFilePath } from '../../lib/lwfs/lwfs'
import { CommandKind } from './completionkind'
import { CommandKind } from '../completer/completionkind'


export interface AtSuggestionItemEntry {
Expand Down
40 changes: 0 additions & 40 deletions src/providers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import {Command} from './completer/command'
import type {CmdItemEntry} from './completer/command'
import {Environment} from './completer/environment'
import type {EnvItemEntry} from './completer/environment'
import {AtSuggestion} from './completer/atsuggestion'
import {LabelDefinition} from './completer/labeldefinition'
import {Package} from './completer/package'
import {Input, Import, SubImport} from './completer/input'
import {Glossary} from './completer/glossary'
import {escapeRegExp} from '../utils/utils'
import { readFilePath } from '../lib/lwfs/lwfs'
import { BracketReplacer } from './completer/bracketreplacer'
import { CommandRemover } from './completer/commandremover'
Expand Down Expand Up @@ -272,42 +270,4 @@ export class Completer implements vscode.CompletionItemProvider {
}
}

export class AtSuggestionCompleter implements vscode.CompletionItemProvider {
private readonly atSuggestion: AtSuggestion
private readonly triggerCharacter: string

constructor(
extension: {
readonly extensionRoot: string
},
triggerCharacter: string
) {
this.atSuggestion = new AtSuggestion(extension, triggerCharacter)
this.triggerCharacter = triggerCharacter
}

get readyPromise() {
return this.atSuggestion.readyPromise
}

provideCompletionItems(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken,
context: vscode.CompletionContext
): vscode.CompletionItem[] | undefined {
const line = document.lineAt(position).text.substring(0, position.character)
return this.completion(line, {document, position, token, context})
}

private completion(line: string, args: {document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext}): vscode.CompletionItem[] {
const escapedTriggerCharacter = escapeRegExp(this.triggerCharacter)
const reg = new RegExp(escapedTriggerCharacter + '[^\\\\s]*$')
const result = line.match(reg)
let suggestions: vscode.CompletionItem[] = []
if (result) {
suggestions = this.atSuggestion.provideFrom(result, args)
}
return suggestions
}
}
2 changes: 1 addition & 1 deletion src/providersmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { FoldingProvider } from './providers/folding'
import { BibtexFormatterProvider } from './providers/bibtexformatter'
import type { Extension } from './main'
import { MathPreviewPanelSerializer } from './components/mathpreviewpanel'
import { AtSuggestionCompleter } from './providers/completion'
import { AtSuggestionCompleter } from './providers/atsuggestion'


abstract class SingleProviderManager implements vscode.Disposable {
Expand Down
9 changes: 7 additions & 2 deletions test/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
runTestWithFixture,
sleep
} from './utils/ciutils'
import { AtSuggestionCompleter } from '../src/providers/atsuggestion'


function assertCompletionItemContainsSnippet(items: vscode.CompletionItem[], prefix: string, snippet: string): void {
Expand Down Expand Up @@ -78,7 +79,9 @@ suite('Completion test suite', () => {
await sleep(1000)
const pos = new vscode.Position(3,1)
const token = new vscode.CancellationTokenSource().token
const items = extension.exports.realExtension.completer.atSuggestionCompleter.provideCompletionItems(
const atSuggestionCompleter = new AtSuggestionCompleter(extension.exports.realExtension, '@')
await atSuggestionCompleter.readyPromise
const items = atSuggestionCompleter.provideCompletionItems(
doc, pos, token,
{
triggerKind: vscode.CompletionTriggerKind.Invoke,
Expand All @@ -104,7 +107,9 @@ suite('Completion test suite', () => {
await sleep(1000)
const pos = new vscode.Position(3,1)
const token = new vscode.CancellationTokenSource().token
const items = extension.exports.realExtension.completer.atSuggestionCompleter.provideCompletionItems(
const atSuggestionCompleter = new AtSuggestionCompleter(extension.exports.realExtension, '@')
await atSuggestionCompleter.readyPromise
const items = atSuggestionCompleter.provideCompletionItems(
doc, pos, token,
{
triggerKind: vscode.CompletionTriggerKind.Invoke,
Expand Down

0 comments on commit 9f1c203

Please sign in to comment.