Skip to content

Commit

Permalink
Restore the original word-wise movement behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
lhecker committed Aug 1, 2023
1 parent a5663f1 commit 78fd2f4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
38 changes: 19 additions & 19 deletions src/host/cmdline.cpp
Expand Up @@ -2,29 +2,16 @@
// Licensed under the MIT license.

#include "precomp.h"

#include "cmdline.h"

#include "_output.h"
#include "output.h"
#include "stream.h"
#include "_stream.h"
#include "dbcs.h"
#include "handle.h"
#include "misc.h"
#include "../types/inc/convert.hpp"
#include "srvinit.h"

#include "ApiRoutines.h"

#include "../interactivity/inc/ServiceLocator.hpp"

#pragma hdrstop
using Microsoft::Console::Interactivity::ServiceLocator;

// Routine Description:
// - Detects Word delimiters
bool IsWordDelim(const wchar_t wch)
bool IsWordDelim(const wchar_t wch) noexcept
{
// the space character is always a word delimiter. Do not add it to the WordDelimiters global because
// that contains the user configurable word delimiters only.
Expand All @@ -33,14 +20,27 @@ bool IsWordDelim(const wchar_t wch)
return true;
}
const auto& delimiters = ServiceLocator::LocateGlobals().WordDelimiters;
if (delimiters.empty())
{
return false;
}
return std::ranges::find(delimiters, wch) != delimiters.end();
}

bool IsWordDelim(const std::wstring_view charData)
bool IsWordDelim(const std::wstring_view& charData) noexcept
{
return charData.size() == 1 && IsWordDelim(charData.front());
}

// Returns a truthy value for delimiters and 0 otherwise.

This comment has been minimized.

Copy link
@DHowett

DHowett Aug 1, 2023

Member

yay classes

// The distinction between whitespace and other delimiters allows us to
// implement Windows' inconsistent, but classic, word-wise navigation.
int DelimiterClass(wchar_t wch) noexcept
{
if (wch == L' ')
{
return 1;
}
const auto& delimiters = ServiceLocator::LocateGlobals().WordDelimiters;
if (std::find(delimiters.begin(), delimiters.end(), wch) != delimiters.end())
{
return 2;
}
return 0;
}
5 changes: 3 additions & 2 deletions src/host/cmdline.h
Expand Up @@ -6,5 +6,6 @@
#include "screenInfo.hpp"

// Word delimiters
bool IsWordDelim(const wchar_t wch);
bool IsWordDelim(const std::wstring_view charData);
bool IsWordDelim(wchar_t wch) noexcept;
bool IsWordDelim(const std::wstring_view& charData) noexcept;
int DelimiterClass(wchar_t wch) noexcept;
20 changes: 12 additions & 8 deletions src/host/readDataCooked.cpp
Expand Up @@ -366,11 +366,13 @@ void COOKED_READ_DATA::_handleVkey(uint16_t vkey, DWORD modifiers)
{
if (ctrlPressed)
{
// This would ideally use GraphemePrev() as well, but IsWordDelim() hasn't been refactored yet.
// Seek to the preceding left-hand word boundary.
// (The boundary between a non-word on the left and word on the right.)
--_bufferCursor;
while (_bufferCursor != 0 && (!IsWordDelim(_buffer[_bufferCursor - 1]) || IsWordDelim(_buffer[_bufferCursor])))
while (_bufferCursor != 0 && _buffer[_bufferCursor] == L' ')
{
--_bufferCursor;
}
const auto dc = DelimiterClass(_buffer[_bufferCursor]);
while (_bufferCursor != 0 && DelimiterClass(_buffer[_bufferCursor - 1]) == dc)
{
--_bufferCursor;
}
Expand All @@ -388,11 +390,13 @@ void COOKED_READ_DATA::_handleVkey(uint16_t vkey, DWORD modifiers)
{
if (ctrlPressed && vkey == VK_RIGHT)
{
// This would ideally use GraphemeNext() as well, but IsWordDelim() hasn't been refactored yet.
// Seek to the preceding left-hand word boundary.
// (The boundary between a non-word on the left and word on the right.)
++_bufferCursor;
while (_bufferCursor != _buffer.size() && (!IsWordDelim(_buffer[_bufferCursor - 1]) || IsWordDelim(_buffer[_bufferCursor])))
const auto dc = DelimiterClass(_buffer[_bufferCursor - 1]);
while (_bufferCursor != _buffer.size() && dc == DelimiterClass(_buffer[_bufferCursor]))
{
++_bufferCursor;
}
while (_bufferCursor != _buffer.size() && _buffer[_bufferCursor] == L' ')
{
++_bufferCursor;
}
Expand Down

0 comments on commit 78fd2f4

Please sign in to comment.