-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from yanxiaodi/task-4
Add select-hover-translation
- Loading branch information
Showing
14 changed files
with
467 additions
and
271 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
// Place your settings in this file to overwrite default and user settings. | ||
{ | ||
"files.exclude": { | ||
"out": false, // set this to true to hide the "out" folder with the compiled JS files | ||
"dist": false // set this to true to hide the "dist" folder with the compiled JS files | ||
}, | ||
"search.exclude": { | ||
"out": true, // set this to false to include "out" folder in search results | ||
"dist": true // set this to false to include "dist" folder in search results | ||
}, | ||
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts | ||
"typescript.tsc.autoDetect": "off" | ||
} | ||
"files.exclude": { | ||
"out": false, // set this to true to hide the "out" folder with the compiled JS files | ||
"dist": false // set this to true to hide the "dist" folder with the compiled JS files | ||
}, | ||
"search.exclude": { | ||
"out": true, // set this to false to include "out" folder in search results | ||
"dist": true // set this to false to include "dist" folder in search results | ||
}, | ||
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts | ||
"typescript.tsc.autoDetect": "off", | ||
"commentTranslate.multiLineMerge": true, | ||
"commentTranslate.hover.enabled": true | ||
} |
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,9 +1,18 @@ | ||
# Change Log | ||
|
||
All notable changes to the "translator-helper" extension will be documented in this file. | ||
## [0.3.0] - 2022-05-09 | ||
|
||
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. | ||
- Automatically copy the translated text to clipboard for the `translate` command. | ||
- Support selection hover translation. You can select a word or a sentence, then you will see a popup with the translated text. Click **Copy** to copy the translated text. You can turn off this feature in the settings. | ||
|
||
## [Unreleased] | ||
## [0.2.0] - 2022-05-08 | ||
|
||
- Initial release | ||
- Update dependencies. | ||
|
||
## [0.1.0] | ||
|
||
- Add reverse translation. | ||
|
||
## [0.0.3] | ||
|
||
- Initial release. |
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,121 @@ | ||
import { getContent } from './store'; | ||
import { CommandIds } from './consts'; | ||
import { ITranslatorService } from './translate-service'; | ||
import { | ||
getParagraph, | ||
getSelectionText, | ||
insertText, | ||
setCurrentEditor, | ||
} from './doc-service'; | ||
import { commands, env, ExtensionContext, StatusBarItem, window } from 'vscode'; | ||
|
||
export const regiserCommands = ( | ||
context: ExtensionContext, | ||
servie: ITranslatorService, | ||
source: string, | ||
target: string, | ||
statusBarItem: StatusBarItem | ||
) => { | ||
let translateInsert = commands.registerCommand( | ||
CommandIds.translateInsertCommand, | ||
async () => await translateInsertCommand(servie, source, target) | ||
); | ||
let reverseTranslateInsert = commands.registerCommand( | ||
CommandIds.reverseTranslateInsertCommand, | ||
async () => await reverseTranslateInsertCommand(servie, source, target) | ||
); | ||
|
||
let translate = commands.registerCommand( | ||
CommandIds.translateCommand, | ||
async () => await translateCommand(servie, source, target, statusBarItem) | ||
); | ||
|
||
let copyTranslationText = commands.registerCommand( | ||
CommandIds.copyTranslationTextCommand, | ||
copyTranslationTextCommand | ||
); | ||
context.subscriptions.push( | ||
translateInsert, | ||
reverseTranslateInsert, | ||
translate, | ||
copyTranslationText | ||
); | ||
}; | ||
|
||
export const translateInsertCommand = async ( | ||
translatorService: ITranslatorService, | ||
source: string, | ||
target: string | ||
) => { | ||
// The code you place here will be executed every time your command is executed | ||
setCurrentEditor(); | ||
const text = getParagraph(); | ||
try { | ||
if (text.trim() !== '') { | ||
let result = await translatorService.translate(text, source, target); | ||
insertText(result); | ||
} | ||
} catch (error: any) { | ||
window.showErrorMessage(`Error occurs. ${error}`); | ||
} | ||
}; | ||
|
||
export const reverseTranslateInsertCommand = async ( | ||
translatorService: ITranslatorService, | ||
source: string, | ||
target: string | ||
) => { | ||
// The code you place here will be executed every time your command is executed | ||
setCurrentEditor(); | ||
const text = getParagraph(); | ||
try { | ||
if (text.trim() !== '') { | ||
let result = await translatorService.translate(text, target, source); | ||
insertText(result); | ||
} | ||
} catch (error: any) { | ||
window.showErrorMessage(`Error occurs. ${error}`); | ||
} | ||
}; | ||
|
||
export const translateCommand = async ( | ||
translatorService: ITranslatorService, | ||
source: string, | ||
target: string, | ||
statusBarItem: StatusBarItem | ||
) => { | ||
// The code you place here will be executed every time your command is executed | ||
setCurrentEditor(); | ||
const text = getSelectionText(); | ||
try { | ||
if (text.trim() !== '') { | ||
const result = await translatorService.translate(text, source, target); | ||
statusBarItem.hide(); | ||
statusBarItem.text = `$(book) ${result}`; | ||
statusBarItem.show(); | ||
// Copy to clipboard | ||
copyToClipboard(result); | ||
} else { | ||
statusBarItem.hide(); | ||
} | ||
} catch (error: any) { | ||
window.showErrorMessage(`Error occurs. ${error}`); | ||
} | ||
|
||
// Display a message box to the user | ||
//vscode.window.showInformationMessage('Hello World!'); | ||
}; | ||
|
||
export const copyTranslationTextCommand = () => { | ||
try { | ||
var content = getContent(); | ||
copyToClipboard(content); | ||
} catch (error: any) { | ||
window.showErrorMessage(`Error occurs. ${error}`); | ||
} | ||
}; | ||
|
||
const copyToClipboard = (content: string) => { | ||
env.clipboard.writeText(content); | ||
window.showInformationMessage(`Translation text copyied to the clipboard!`); | ||
}; |
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,21 @@ | ||
import { workspace } from 'vscode'; | ||
|
||
export const getConfiguration = <T>(key: string) => { | ||
return workspace.getConfiguration('translatorHelper').get<T>(key); | ||
}; | ||
|
||
export const getApiConfiguration = () => { | ||
return getConfiguration<string>('api') ?? 'google'; | ||
}; | ||
|
||
export const getSourceLanguageConfiguration = () => { | ||
return getConfiguration<string>('sourceLanguage') ?? 'en'; | ||
}; | ||
|
||
export const getTargetLanguageConfiguration = () => { | ||
return getConfiguration<string>('targetLanguage') ?? 'zh-CN'; | ||
}; | ||
|
||
export const getEnableSelectionHoverTranslationConfiguration = () => { | ||
return getConfiguration<boolean>('enableSelectionHoverTranslation') ?? true; | ||
}; |
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,6 @@ | ||
export enum CommandIds { | ||
translateInsertCommand = 'translatorHelper.translateInsert', | ||
reverseTranslateInsertCommand = 'translatorHelper.reverseTranslateInsert', | ||
translateCommand = 'translatorHelper.translate', | ||
copyTranslationTextCommand = 'translatorHelper.copyTranslationText', | ||
} |
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,67 @@ | ||
import { Position, Selection, TextEditor, window } from 'vscode'; | ||
|
||
let editor: TextEditor | undefined; | ||
|
||
export const setCurrentEditor = () => { | ||
editor = window.activeTextEditor; | ||
}; | ||
|
||
export const getParagraph = (): string => { | ||
if (editor !== undefined) { | ||
let startLine = editor.selection.start.line; | ||
let endLine = editor.selection.end.line; | ||
const endCharacter = editor.document.lineAt(endLine).text.length; | ||
editor.selection = new Selection(startLine, 0, startLine, endCharacter); | ||
var paragraph = editor.selection; | ||
let result = editor.document.getText(paragraph); | ||
if (result !== undefined) { | ||
return result; | ||
} else { | ||
return ''; | ||
} | ||
} else { | ||
return ''; | ||
} | ||
}; | ||
|
||
export const getSelectionText = (): string => { | ||
if (editor !== undefined) { | ||
return editor.document.getText(editor.selection); | ||
} else { | ||
return ''; | ||
} | ||
}; | ||
|
||
export const insertText = (text: string): void => { | ||
if (editor !== undefined) { | ||
let end = editor.selection.end; | ||
editor | ||
.edit((editBuilder) => { | ||
editBuilder.insert(end, '\n'); | ||
editBuilder.insert(end, text); | ||
}) | ||
.then((success) => { | ||
if (success && editor !== undefined) { | ||
let end = editor.selection.end; | ||
editor.selection = new Selection(end, end); | ||
let startLine = editor.selection.start.line; | ||
let endLine = editor.selection.end.line; | ||
const endCharacter = editor.document.lineAt(endLine).text.length; | ||
editor.selection = new Selection( | ||
startLine, | ||
0, | ||
startLine, | ||
endCharacter | ||
); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
export const isHoverOnSelection = (position: Position): boolean => { | ||
return ( | ||
editor !== undefined && | ||
!editor.selection.isEmpty && | ||
editor.selection.contains(position) | ||
); | ||
}; |
Oops, something went wrong.