Created
April 15, 2022 00:42
-
-
Save sandipchitale/8dc39e7d80628dba899acdc777a6ff23 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* eslint-disable @typescript-eslint/naming-convention */ | |
import * as vscode from 'vscode'; | |
import * as path from 'path'; | |
import * as fs from 'fs'; | |
const BRANCHES = [ | |
'Main', | |
'rel_IA_22.2', | |
'rel_IA_21.4', | |
'rel_IA_21.2', | |
'rel_IA_20.4', | |
'rel_IA_20.2', | |
'rel_IA_16EP7', | |
'rel_IA_16EP5.1', | |
'rel_IA_16EP5', | |
'rel_IA_22.3' | |
]; | |
let currentBranch: string | undefined; | |
let currentWorkspaceFolderPath: string | undefined; | |
type LANGUAGE_IDS = 'de' | 'en' | 'es' | 'fr' | 'it' | 'ja' | 'nl' | 'zh'; | |
const EN_DOT_JSON_PATHS: any = { | |
'infoarchiveng': path.join('src', 'frontendng', 'src', 'languages', 'en.json'), | |
'infoarchive': path.join('src', 'frontend' , 'src', 'languages', 'en.json') // rel_IA_16EP5.1 and before | |
}; | |
const LANGUAGE_ID_DOT_JSON_PATHS: any = { | |
'infoarchiveng': path.join('..', '..', '..', 'localization', '{LANGUAGE_ID}', 'WEB-INF', 'classes', 'static', 'languages', '{LANGUAGE_ID}.json'), | |
'infoarchive': path.join('..', '..', '..', 'localization', '{LANGUAGE_ID}', 'WEB-INF', 'classes', 'static', 'languages', '{LANGUAGE_ID}.json') // rel_IA_16EP5.1 and before | |
}; | |
let language_id: LANGUAGE_IDS = 'zh'; | |
let translations: any; | |
export function activate(context: vscode.ExtensionContext) { | |
const workspaceFolder = vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0]; | |
if (workspaceFolder) { | |
currentWorkspaceFolderPath = workspaceFolder.uri.fsPath; | |
if (currentWorkspaceFolderPath) { | |
BRANCHES.forEach((branch) => { | |
// Is branchname in path | |
if (currentWorkspaceFolderPath?.indexOf(`${path.sep}${branch}${path.sep}`) !== -1) { | |
currentBranch = branch; | |
} | |
}); | |
// On one of the IA branches | |
if (currentBranch) { | |
const projectname = path.basename(currentWorkspaceFolderPath); | |
if (projectname && (projectname === 'infoarchiveng') || (projectname === 'infoarchive')) { | |
const languageIdDotJsonRelativePath = | |
(language_id === 'en') ? EN_DOT_JSON_PATHS[projectname] : LANGUAGE_ID_DOT_JSON_PATHS[projectname].replaceAll('{LANGUAGE_ID}', language_id); | |
if (languageIdDotJsonRelativePath) { | |
let jsonText = fs.readFileSync(path.join(currentWorkspaceFolderPath, languageIdDotJsonRelativePath), {encoding:'utf8', flag:'r'}); | |
if (jsonText.charCodeAt(0) === 65279) { | |
// Remove BOM marker if any | |
jsonText = jsonText.substring(1); | |
} | |
// Load translations | |
translations = JSON.parse(jsonText); | |
// Load register hover provider | |
vscode.languages.registerHoverProvider({ language: 'html', scheme: 'file' }, { | |
provideHover: hoverProvider | |
}); | |
vscode.languages.registerHoverProvider({ language: 'typescript', scheme: 'file' }, { | |
provideHover: hoverProvider | |
}); | |
vscode.languages.registerHoverProvider({ language: 'javascript', scheme: 'file' }, { | |
provideHover: hoverProvider | |
}); | |
} | |
} | |
} | |
} | |
} | |
} | |
async function hoverProvider(textDocument: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken) { | |
if (translations && currentWorkspaceFolderPath) { | |
if (textDocument.uri.fsPath.startsWith(path.join(currentWorkspaceFolderPath, path.join('src', 'frontendng', 'src', 'app')))) { | |
if (vscode.window.activeTextEditor) { | |
const wordRange = textDocument.getWordRangeAtPosition(vscode.window.activeTextEditor.selection.active); | |
if (wordRange) { | |
const startLine = wordRange.start.line; | |
const characterMinusOne = wordRange.start.character - 1; | |
const charBefore = textDocument.getText(wordRange.with(wordRange.start.with(startLine, characterMinusOne), wordRange.start)); | |
const endLine = wordRange.end.line; | |
const characterPlusOne = wordRange.end.character + 1; | |
const charAfter = textDocument.getText(wordRange.with(wordRange.end, wordRange.end.with(endLine, characterPlusOne) )); | |
if ((charBefore === '"' || charBefore === '\'') && (charAfter === '"' || charAfter === '\'')) { | |
// word is preceded by quotes | |
const word = textDocument.getText(wordRange); | |
if (word) { | |
const wordTranslation = translations[word]; | |
if (wordTranslation) { | |
return new vscode.Hover(wordTranslation); | |
} else { | |
return new vscode.Hover(`No translation found for: ${word}`); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
return new vscode.Hover(''); | |
} | |
export function deactivate() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment