Skip to content

Commit

Permalink
Working with cursor color change issue.
Browse files Browse the repository at this point in the history
Not happy with the implementation yet.
Also added new option to set cursor shape when selection is active in
normal mode.
  • Loading branch information
johtela committed Jun 10, 2020
1 parent ac985cf commit 2fbdf17
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 45 deletions.
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,35 @@
"default": "underline",
"description": "Shape of the cursor when incremental search is active."
},
"modaledit.selectCursorStyle": {
"type": "string",
"enum": [
"block",
"block-outline",
"line",
"line-thin",
"underline",
"underline-thin"
],
"default": "line-thin",
"description": "Shape of the cursor when selection is active in normal mode."
},
"modaledit.insertCursorColor": {
"type": "string",
"description": "Color of the cursor in insert mode (in HTML format)."
},
"modaledit.normalCursorColor": {
"type": "string",
"description": "Color of the cursor in normal mode (in HTML format)."
},
"modaledit.searchCursorColor": {
"type": "string",
"description": "Color of the cursor when search is active (in HTML format)."
},
"modaledit.selectCursorColor": {
"type": "string",
"description": "Color of the cursor when selection is active in normal mode (in HTML format)."
},
"modaledit.startInNormalMode": {
"type": "boolean",
"default": true,
Expand Down
38 changes: 31 additions & 7 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ type Cursor =
let insertCursorStyle: vscode.TextEditorCursorStyle
let normalCursorStyle: vscode.TextEditorCursorStyle
let searchCursorStyle: vscode.TextEditorCursorStyle
let selectCursorStyle: vscode.TextEditorCursorStyle
let insertCursorColor: string | undefined
let normalCursorColor: string | undefined
let searchCursorColor: string | undefined
let selectCursorColor: string | undefined
/**
* Another thing you can set in config, is whether ModalEdit starts in normal
* mode.
Expand Down Expand Up @@ -130,16 +135,29 @@ let keymapsById: { [id: number]: Keymap }
*
* The following functions return the current configuration settings.
*/
export function getInsertCursorStyle(): vscode.TextEditorCursorStyle {
return insertCursorStyle
export function getInsertCursorStyle():
[vscode.TextEditorCursorStyle, string | undefined] {
return [ insertCursorStyle, insertCursorColor ]
}

export function getNormalCursorStyle(): vscode.TextEditorCursorStyle {
return normalCursorStyle
export function getNormalCursorStyle():
[vscode.TextEditorCursorStyle, string | undefined ] {
return [ normalCursorStyle, normalCursorColor ]
}

export function getSearchCursorType(): vscode.TextEditorCursorStyle {
return searchCursorStyle
export function getSearchCursorStyle():
[vscode.TextEditorCursorStyle, string | undefined] {
return [ searchCursorStyle, searchCursorColor ]
}

export function getSelectCursorStyle():
[vscode.TextEditorCursorStyle, string | undefined] {
return [ selectCursorStyle, selectCursorColor ]
}

export function cursorColorCustomized(): boolean {
return insertCursorColor || normalCursorColor || searchCursorColor ||
selectCursorColor ? true : false
}

export function getStartInNormalMode(): boolean {
Expand Down Expand Up @@ -187,6 +205,12 @@ export function updateFromConfig(): void {
config.get<Cursor>("normalCursorStyle", "block"))
searchCursorStyle = toVSCursorStyle(
config.get<Cursor>("searchCursorStyle", "underline"))
selectCursorStyle = toVSCursorStyle(
config.get<Cursor>("selectCursorStyle", "line-thin"))
insertCursorColor = config.get("insertCursorColor") || undefined
normalCursorColor = config.get("normalCursorColor") || undefined
searchCursorColor = config.get("searchCursorColor") || undefined
selectCursorColor = config.get("selectCursorColor") || undefined
startInNormalMode = config.get<boolean>("startInNormalMode", true)
}
/**
Expand Down Expand Up @@ -514,7 +538,7 @@ export async function handleKey(key: string, selecting: boolean,

if (!currentKeymap) {
keySeqStack.push(keySequence)
keySequence = []
keySequence = []
}
keySequence.push(key)
if (capture && lastCommand)
Expand Down
85 changes: 50 additions & 35 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ let searching = false
*/
let searchString: string
let searchStartSelections: vscode.Selection[]
let searchWrapped = false
let searchInfo: string | null = null
/**
* Current search parameters.
*/
Expand Down Expand Up @@ -171,6 +171,10 @@ let textChanged = false
let currentKeySequence: string[] = []
let lastKeySequence: string[] = []
let lastChange: string[] = []
/**
* The workbench configuration is needed for changing cursor color.
*/
let workbenchConfig = vscode.workspace.getConfiguration('workbench')
/**
* ## Command Names
*
Expand Down Expand Up @@ -248,7 +252,7 @@ async function onType(event: { text: string }) {
lastKeySequence = currentKeySequence
currentKeySequence = []
}
updateStatusBar(vscode.window.activeTextEditor, actions.getHelp())
updateCursorAndStatusBar(vscode.window.activeTextEditor, actions.getHelp())
}
/**
* Whenever text changes in an active editor, we set a flag. This flag is
Expand Down Expand Up @@ -330,28 +334,16 @@ async function setNormalMode(value: boolean): Promise<void> {
}
}
/**
* This function updates the cursor shape according to the mode. It delegates
* updating of the status bar to the next subroutine.
*/
export function updateCursorAndStatusBar(editor: vscode.TextEditor | undefined) {
if (editor)
editor.options.cursorStyle =
searching ? actions.getSearchCursorType() :
normalMode ? actions.getNormalCursorStyle() :
actions.getInsertCursorStyle()
selecting = false
updateStatusBar(editor)
}
/**
* The last function updates the status bar text according to the mode. It also
* indicates if selection is active or if search mode on. If so, it shows the
* search parameters. If no editor is active, we hide the status bar item.
* This function updates the cursor shape and status bar according to editor
* state. It indicates when selection is active or search mode is on. If
* so, it shows the search parameters. If no editor is active, we hide the
* status bar items.
*/
export function updateStatusBar(editor: vscode.TextEditor | undefined,
export function updateCursorAndStatusBar(editor: vscode.TextEditor | undefined,
help?: string) {
if (editor) {
/**
* Update main status bar.
* Update the main status bar.
*/
let prim: string
if (searching)
Expand All @@ -366,23 +358,32 @@ export function updateStatusBar(editor: vscode.TextEditor | undefined,
/**
* Update secondary status bar. If there is any keys pressed in the
* current sequence, we show them. Also possible help string is shown.
* The info about search wrapping around is shown only as long there are
* The info given by search command is shown only as long there are
* no other messages to show.
*/
let sec = " " + currentKeySequence.join("")
if (help)
sec = `${sec} - ${help}`
if (searchWrapped) {
if (sec.trim() == "") {
let limit = (bw: boolean) => bw ? "TOP" : "BOTTOM"
sec = `Search hit ${limit(searchBackwards)} continuing at ${
limit(!searchBackwards)}`
}
sec = `${sec} ${help}`
if (searchInfo) {
if (sec.trim() == "")
sec = searchInfo
else
searchWrapped = false
searchInfo = null
}
secondaryStatusBar.text = sec
secondaryStatusBar.show()
/**
* Update the cursor(s).
*/
let [style, color] =
searching ? actions.getSearchCursorStyle() :
selecting && normalMode ? actions.getSelectCursorStyle() :
normalMode ? actions.getNormalCursorStyle() :
actions.getInsertCursorStyle()
editor.options.cursorStyle = style
if (actions.cursorColorCustomized())
workbenchConfig.update('colorCustomizations',
{ "editorCursor.foreground": color })
}
else {
mainStatusBar.hide()
Expand All @@ -401,7 +402,7 @@ async function cancelSelection(): Promise<void> {
if (selecting) {
selecting = false
await vscode.commands.executeCommand("cancelSelection")
updateStatusBar(vscode.window.activeTextEditor)
updateCursorAndStatusBar(vscode.window.activeTextEditor)
}
}
/**
Expand All @@ -414,7 +415,7 @@ async function toggleSelection(): Promise<void> {
if (oldSelecting)
await vscode.commands.executeCommand("cancelSelection")
selecting = !oldSelecting
updateStatusBar(vscode.window.activeTextEditor)
updateCursorAndStatusBar(vscode.window.activeTextEditor)
}
/**
* The following helper function actually determines, if a selection is active.
Expand All @@ -428,6 +429,15 @@ function isSelecting(): boolean {
selection => !selection.anchor.isEqual(selection.active))
return selecting
}
/**
* Function that sets the selecting flag off. This function is called from one
* event. The flag is resetted when the active editor changes. The function that
* updates the status bar sets the flag on again, if there are any active
* selections.
*/
export function resetSelecting() {
selecting = false
}
/**
* ## Search Commands
*
Expand Down Expand Up @@ -517,7 +527,7 @@ async function search(args: SearchArgs | string): Promise<void> {
*/
function highlightMatches(editor: vscode.TextEditor,
selections: vscode.Selection[]) {
searchWrapped = false
searchInfo = null
if (searchString == "")
/**
* If search string is empty, we return to the start positions.
Expand Down Expand Up @@ -562,9 +572,14 @@ function highlightMatches(editor: vscode.TextEditor,
offs = searchBackwards ?
docText.lastIndexOf(target) :
docText.indexOf(target)
if (offs < 0)
if (offs < 0) {
searchInfo = "Pattern not found"
return sel
searchWrapped = true
}
let limit = (bw: boolean) => bw ? "TOP" : "BOTTOM"
searchInfo =
`Search hit ${limit(searchBackwards)} continuing at ${
limit(!searchBackwards)}`
}
/**
* If search was successful, we return a new selection to highlight
Expand Down Expand Up @@ -642,7 +657,7 @@ function deleteCharFromSearch() {
if (editor && searching && searchString.length > 0) {
searchString = searchString.slice(0, searchString.length - 1)
highlightMatches(editor, searchStartSelections)
updateStatusBar(editor)
updateCursorAndStatusBar(editor)
}
}
/**
Expand Down
5 changes: 2 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
channel,
vscode.workspace.onDidChangeConfiguration(actions.updateFromConfig),
vscode.window.onDidChangeVisibleTextEditors(editors =>
editors.forEach(commands.updateCursorAndStatusBar)),
vscode.window.onDidChangeVisibleTextEditors(commands.resetSelecting),
vscode.window.onDidChangeTextEditorSelection(e =>
commands.updateStatusBar(e.textEditor)),
commands.updateCursorAndStatusBar(e.textEditor)),
vscode.workspace.onDidChangeTextDocument(commands.onTextChanged))
/**
* Next we update the active settings from the config file, and at last,
Expand Down

0 comments on commit 2fbdf17

Please sign in to comment.