forked from mattbierner/vscode-emojisense
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
367010a
commit 4cc940b
Showing
10 changed files
with
147 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import * as vscode from "vscode" | ||
import { EmojiProvider, Emoji } from './emoji' | ||
import Configuration from './configuration' | ||
|
||
const Datauri = require('datauri') | ||
|
||
export default class DecoratorProvider extends vscode.Disposable { | ||
|
||
private readonly disposables: vscode.Disposable[] | ||
|
||
private readonly decorationType: vscode.TextEditorDecorationType | ||
|
||
private activeEditor: vscode.TextEditor | undefined = undefined | ||
private timeout: any; | ||
|
||
constructor( | ||
private readonly emojiProvider: EmojiProvider, | ||
private readonly config: Configuration | ||
) { | ||
super(() => this.dispose()) | ||
this.decorationType = vscode.window.createTextEditorDecorationType({}) | ||
|
||
this.activeEditor = vscode.window.activeTextEditor | ||
this.setDecorators(this.activeEditor) | ||
|
||
vscode.window.onDidChangeActiveTextEditor(editor => { | ||
this.activeEditor = editor | ||
if (editor) { | ||
this.triggerUpdateDecorations() | ||
} | ||
}, this, this.disposables) | ||
|
||
vscode.workspace.onDidChangeTextDocument((event: vscode.TextDocumentChangeEvent) => { | ||
if (this.activeEditor && event.document === this.activeEditor.document) { | ||
this.triggerUpdateDecorations(); | ||
} | ||
}, this, this.disposables) | ||
} | ||
|
||
dispose() { | ||
let d: vscode.Disposable | undefined = undefined | ||
while ((d = this.disposables.pop())) { | ||
d.dispose() | ||
} | ||
} | ||
|
||
private triggerUpdateDecorations(): void { | ||
if (this.timeout) { | ||
return | ||
} | ||
this.timeout = setTimeout(() => { | ||
this.setDecorators(this.activeEditor) | ||
this.timeout = null | ||
}, 300) | ||
} | ||
|
||
private setDecorators(activeEditor: vscode.TextEditor | undefined) { | ||
if (!activeEditor || !this.config.isInlineEnabled(activeEditor.document.languageId)) { | ||
return false | ||
} | ||
|
||
const regEx = /:([\w\d_\+\-]+?):/g; | ||
const text = activeEditor.document.getText(); | ||
let match; | ||
const d: vscode.DecorationOptions[] = [] | ||
while (match = regEx.exec(text)) { | ||
const name = match[1] | ||
const emoji = this.emojiProvider.lookup(name) | ||
if (!emoji) { | ||
continue | ||
} | ||
|
||
const startPos = activeEditor.document.positionAt(match.index + 1); | ||
const endPos = activeEditor.document.positionAt(match.index + match[0].length - 1); | ||
d.push({ | ||
range: new vscode.Range(startPos, endPos), | ||
hoverMessage: this.hoverMessage(emoji), | ||
renderOptions: { | ||
after: { | ||
contentText: emoji.emoji, | ||
margin: '0.2em', | ||
color: 'rgba(255, 255, 255, 0.4)' | ||
} | ||
} | ||
}) | ||
} | ||
activeEditor.setDecorations(this.decorationType, d); | ||
} | ||
|
||
private hoverMessage(emoji: Emoji): string { | ||
const width = 160 | ||
const height = 160 | ||
const datauri = new Datauri(); | ||
const src = `<?xml version="1.0" encoding="utf-8"?> | ||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> | ||
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="${width}px" height="${height}px" viewBox="0 0 ${width} ${height}" xml:space="preserve"> | ||
<text x="50%" y="50%" text-anchor="middle" alignment-baseline="central" font-size="120">${emoji.emoji}</text> | ||
</svg>`; | ||
datauri.format('.svg', src); | ||
return `` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,38 @@ | ||
import * as vscode from 'vscode' | ||
import EmojiCompletionProvider from './EmojiCompletionProvider' | ||
import EmojiHoverProvider from './EmojiHoverProvider'; | ||
import { EmojiProvider } from './emoji' | ||
import Configuration from './configuration' | ||
import DecoratorProvider from "./DecoratorProvider"; | ||
|
||
function registerProviders( | ||
provider: EmojiCompletionProvider, | ||
hoverProvider: EmojiHoverProvider, | ||
config: Configuration | ||
): vscode.Disposable { | ||
const completions: vscode.Disposable[] = [] | ||
const hovers: vscode.Disposable[] = [] | ||
const disposables: vscode.Disposable[] = [] | ||
for (const language of config.languages) { | ||
if (config.shouldShowOnColon(language)) { | ||
completions.push(vscode.languages.registerCompletionItemProvider(language, provider, ':')) | ||
disposables.push(vscode.languages.registerCompletionItemProvider(language, provider, ':')) | ||
} else { | ||
completions.push(vscode.languages.registerCompletionItemProvider(language, provider)) | ||
} | ||
|
||
if (config.isHoverEnabled(language)) { | ||
completions.push(vscode.languages.registerHoverProvider(language, hoverProvider)) | ||
disposables.push(vscode.languages.registerCompletionItemProvider(language, provider)) | ||
} | ||
} | ||
|
||
return vscode.Disposable.from(...completions, ...hovers); | ||
return vscode.Disposable.from(...disposables); | ||
} | ||
|
||
|
||
export function activate(context: vscode.ExtensionContext) { | ||
const emoji = new EmojiProvider() | ||
const config = new Configuration() | ||
const provider = new EmojiCompletionProvider(emoji, config) | ||
const hoverProvider = new EmojiHoverProvider(emoji) | ||
|
||
let providerSub = registerProviders(provider, hoverProvider, config) | ||
let providerSub = registerProviders(provider, config) | ||
|
||
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(() => { | ||
vscode.workspace.onDidChangeConfiguration(() => { | ||
config.updateConfiguration() | ||
providerSub.dispose() | ||
providerSub = registerProviders(provider, hoverProvider, config) | ||
})) | ||
providerSub = registerProviders(provider, config) | ||
}, null, context.subscriptions) | ||
|
||
context.subscriptions.push(new DecoratorProvider(emoji, config)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters