Skip to content

Commit

Permalink
Extract AtSuggestionCompleter
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Sep 10, 2023
1 parent c2ad15c commit d88056e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 50 deletions.
53 changes: 53 additions & 0 deletions src/providers/atsuggestion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 {escapeRegExp} from '../../utils/utils'
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 Expand Up @@ -99,4 +99,5 @@ export class AtSuggestion implements IProvider {
})
return this.suggestions
}

}
42 changes: 0 additions & 42 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 @@ -47,7 +45,6 @@ export class Completer implements vscode.CompletionItemProvider {
private readonly import: Import
private readonly subImport: SubImport
readonly glossary: Glossary
readonly atSuggestionCompleter: AtSuggestionCompleter
private readonly bracketReplacer: BracketReplacer
private readonly commandAdder: CommandAdder
private readonly commandRemover: CommandRemover
Expand Down Expand Up @@ -77,7 +74,6 @@ export class Completer implements vscode.CompletionItemProvider {
this.import = new Import(extension)
this.subImport = new SubImport(extension)
this.glossary = new Glossary(extension)
this.atSuggestionCompleter = new AtSuggestionCompleter(extension)
this.bracketReplacer = new BracketReplacer()
this.commandAdder = new CommandAdder(this.command)
this.commandRemover = new CommandRemover()
Expand All @@ -90,7 +86,6 @@ export class Completer implements vscode.CompletionItemProvider {
this.command.readyPromise,
this.environment.readyPromise,
this.package.readyPromise,
this.atSuggestionCompleter.readyPromise
]).then(() => resolve()))
}

Expand Down Expand Up @@ -275,41 +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
}) {
const configuration = vscode.workspace.getConfiguration('latex-toybox')
const triggerCharacter = configuration.get('intellisense.atSuggestion.trigger.latex') as 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
}
}
4 changes: 3 additions & 1 deletion src/providersmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +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/atsuggestion'


abstract class SingleProviderManager implements vscode.Disposable {
Expand Down Expand Up @@ -88,9 +89,10 @@ export class ProvidersManager {
register(): vscode.Disposable {
const configuration = vscode.workspace.getConfiguration('latex-toybox')
const atSuggestionLatexTrigger = configuration.get('intellisense.atSuggestion.trigger.latex', '@')
const atSuggestionCompleter = new AtSuggestionCompleter(extension, atSuggestionLatexTrigger)
return vscode.languages.registerCompletionItemProvider(
latexDoctexSelector,
extension.completer.atSuggestionCompleter,
atSuggestionCompleter,
atSuggestionLatexTrigger
)
}
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
3 changes: 1 addition & 2 deletions test/fixtures/completion/fixture003/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"latex-toybox.intellisense.atSuggestionJSON.replace": {
"@+": "\\sum",
"@8": "",
},
"latex-toybox.intellisense.atSuggestion.trigger.latex": "#"
}
}

0 comments on commit d88056e

Please sign in to comment.