Skip to content

Commit

Permalink
显示翻译和unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jul 8, 2024
1 parent 4e18eac commit f06d530
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/editorTable/languageFeature/documentManager.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import * as vscode from 'vscode';
import { EditorObject } from '../editorTable';
import * as y3 from 'y3-helper';
import { queue } from '../../utility/decorators';

let objectMap: { [key: string]: EditorObject | null } = {};

export async function getObject(uri: vscode.Uri): Promise<EditorObject | undefined> {
if (objectMap[uri.path] === undefined) {
objectMap[uri.path] = await y3.table.getObject(uri) ?? null;
class Manager {
@queue()
async getObject(uri: vscode.Uri): Promise<EditorObject | undefined> {
if (objectMap[uri.path] === undefined) {
objectMap[uri.path] = await y3.table.getObject(uri) ?? null;
}
let object = objectMap[uri.path];
return object ?? undefined;
}
let object = objectMap[uri.path];
return object ?? undefined;
}

const ManagerInstance = new Manager();

export async function getObject(uri: vscode.Uri): Promise<EditorObject | undefined> {
return await ManagerInstance.getObject(uri);
}

export function init() {
Expand Down
60 changes: 57 additions & 3 deletions src/editorTable/languageFeature/hover.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as vscode from 'vscode';
import { getObject } from '../editorTable';
import { getObject } from './documentManager';
import * as jsonc from 'jsonc-parser';
import { language } from 'y3-helper';

class Provider implements vscode.HoverProvider {
class FieldProvider implements vscode.HoverProvider {
async provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken) {
const object = await getObject(document.uri);
if (!object) {
Expand Down Expand Up @@ -41,9 +42,62 @@ class Provider implements vscode.HoverProvider {
}
}

class UnicodeProvider implements vscode.HoverProvider {
async provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken) {
const object = await getObject(document.uri);
if (!object?.tree) {
return;
}

const node = jsonc.findNodeAtOffset(object.tree, document.offsetAt(position));

if (node?.type !== 'string') {
return;
}

const raw = object.json!.slice(node.offset + 1, node.offset + node.length - 1);
if (raw === node.value) {
return;
}

return new vscode.Hover(new vscode.MarkdownString(node.value));
}
}

class TranslateProvider implements vscode.HoverProvider {
async provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken) {
const object = await getObject(document.uri);
if (!object?.tree) {
return;
}

const node = jsonc.findNodeAtOffset(object.tree, document.offsetAt(position));
if (node?.type !== 'number') {
return;
}

const text = language.get(node.value);
if (!text) {
return;
}

return new vscode.Hover(new vscode.MarkdownString(text));
}
}

export function init() {
vscode.languages.registerHoverProvider({
scheme: 'file',
language: 'json',
}, new Provider());
}, new FieldProvider());

vscode.languages.registerHoverProvider({
scheme: 'file',
language: 'json',
}, new UnicodeProvider());

vscode.languages.registerHoverProvider({
scheme: 'file',
language: 'json',
}, new TranslateProvider());
}

0 comments on commit f06d530

Please sign in to comment.