From b39e09f64a319ecaa52a2069b3061a5d2fa8e72b Mon Sep 17 00:00:00 2001 From: cmagnush <34184494+cmagnush@users.noreply.github.com> Date: Sat, 26 Mar 2022 11:31:31 +0100 Subject: [PATCH] Fix NPP 8.3.3 plugin API compatibility Changed length, line and position variables to (u)intptr_t, to support files > 2 GB. This was done based on what the compiler complained about after changing Call functions in ScintillaWindow, so it might not cover everything. --- src/Dialogs/ConsoleDialog.cpp | 16 +++++++-------- src/LuaConsole.cpp | 34 +++++++++++++++---------------- src/LuaScript.cpp | 8 ++++---- src/Npp/Sci_Position.h | 2 +- src/SciTE/GUI.h | 6 +++--- src/SciTE/LuaExtension.cpp | 24 +++++++++++----------- src/SciTE/LuaExtension.h | 2 +- src/SciTE/StyleWriter.cpp | 32 ++++++++++++++--------------- src/SciTE/StyleWriter.h | 38 +++++++++++++++++------------------ 9 files changed, 81 insertions(+), 81 deletions(-) diff --git a/src/Dialogs/ConsoleDialog.cpp b/src/Dialogs/ConsoleDialog.cpp index e36634e..31c3651 100644 --- a/src/Dialogs/ConsoleDialog.cpp +++ b/src/Dialogs/ConsoleDialog.cpp @@ -108,7 +108,7 @@ INT_PTR CALLBACK ConsoleDialog::run_dlgProc(UINT message, WPARAM wParam, LPARAM return FALSE; case WM_SIZE: { RECT rect = { 0, 0, LOWORD(lParam), HIWORD(lParam) }; - int h = min(m_sciInput.Call(SCI_GETLINECOUNT), 8) * m_sciInput.Call(SCI_TEXTHEIGHT, 1); + int h = static_cast(min(m_sciInput.Call(SCI_GETLINECOUNT), 8) * m_sciInput.Call(SCI_TEXTHEIGHT, 1)); MoveWindow((HWND)m_sciOutput.GetID(), 0, 0, rect.right, rect.bottom - h - 14, TRUE); MoveWindow((HWND)m_sciInput.GetID(), 0, rect.bottom - h - 14, rect.right - 50, h + 9, TRUE); m_sciOutput.Call(SCI_DOCUMENTEND); @@ -169,16 +169,16 @@ INT_PTR CALLBACK ConsoleDialog::run_dlgProc(UINT message, WPARAM wParam, LPARAM if (scn->linesAdded != 0) { RECT rect; GetClientRect(_hSelf, &rect); - int h = min(m_sciInput.Call(SCI_GETLINECOUNT), 8) * m_sciInput.Call(SCI_TEXTHEIGHT, 1); + int h = static_cast(min(m_sciInput.Call(SCI_GETLINECOUNT), 8) * m_sciInput.Call(SCI_TEXTHEIGHT, 1)); MoveWindow((HWND)m_sciOutput.GetID(), 0, 0, rect.right, rect.bottom - h - 14, TRUE); MoveWindow((HWND)m_sciInput.GetID(), 0, rect.bottom - h - 14, rect.right - 50, h + 9, TRUE); m_sciOutput.Call(SCI_DOCUMENTEND); } // Not the most efficient way but by far the easiest to do it here - int startLine = 0; - int endLine = m_sciInput.Call(SCI_GETLINECOUNT); - for (int i = startLine; i < endLine; ++i) { + intptr_t startLine = 0; + intptr_t endLine = m_sciInput.Call(SCI_GETLINECOUNT); + for (intptr_t i = startLine; i < endLine; ++i) { m_sciInput.CallString(SCI_MARGINSETTEXT, i, ">"); m_sciInput.Call(SCI_MARGINSETSTYLE, i, STYLE_LINENUMBER); } @@ -299,8 +299,8 @@ LRESULT CALLBACK ConsoleDialog::inputWndProc(HWND hWnd, UINT uMsg, WPARAM wParam } void ConsoleDialog::runStatement() { - int prevLastLine = m_sciOutput.Call(SCI_GETLINECOUNT); - int newLastLine = 0; + intptr_t prevLastLine = m_sciOutput.Call(SCI_GETLINECOUNT); + intptr_t newLastLine = 0; Sci_TextRange tr; tr.chrg.cpMin = 0; @@ -318,7 +318,7 @@ void ConsoleDialog::runStatement() { newLastLine = m_sciOutput.Call(SCI_GETLINECOUNT); - for (int i = prevLastLine; i < newLastLine; ++i) { + for (intptr_t i = prevLastLine; i < newLastLine; ++i) { m_sciOutput.CallString(SCI_MARGINSETTEXT, i - 1, ">"); m_sciOutput.Call(SCI_MARGINSETSTYLE, i - 1, STYLE_LINENUMBER); } diff --git a/src/LuaConsole.cpp b/src/LuaConsole.cpp index 82ebaea..95f8669 100644 --- a/src/LuaConsole.cpp +++ b/src/LuaConsole.cpp @@ -90,7 +90,7 @@ static bool inline isBrace(int ch) { return strchr("[]{}()", ch) != NULL; } -static std::string getRange(GUI::ScintillaWindow *sw, int start, int end) { +static std::string getRange(GUI::ScintillaWindow *sw, intptr_t start, intptr_t end) { if (end <= start) return std::string(); std::vector buffer(end - start + 1); @@ -104,14 +104,14 @@ static std::string getRange(GUI::ScintillaWindow *sw, int start, int end) { return std::string(buffer.begin(), buffer.end() - 1); // don't copy the null } -static std::string getWordAt(GUI::ScintillaWindow *sw, int pos) { - int word_start = sw->Call(SCI_WORDSTARTPOSITION, pos, true); - int word_end = sw->Call(SCI_WORDENDPOSITION, pos, true); +static std::string getWordAt(GUI::ScintillaWindow *sw, intptr_t pos) { + intptr_t word_start = sw->Call(SCI_WORDSTARTPOSITION, pos, true); + intptr_t word_end = sw->Call(SCI_WORDENDPOSITION, pos, true); return getRange(sw, word_start, word_end); } -static std::string getLuaIdentifierAt(GUI::ScintillaWindow *sw, int pos) { - const int line = sw->Call(SCI_LINEFROMPOSITION); +static std::string getLuaIdentifierAt(GUI::ScintillaWindow *sw, intptr_t pos) { + const intptr_t line = sw->Call(SCI_LINEFROMPOSITION); Sci_TextToFind ttf = { { @@ -317,29 +317,29 @@ bool LuaConsole::processNotification(const SCNotification *scn) { } void LuaConsole::maintainIndentation() { - int curPos = m_sciInput->Call(SCI_GETCURRENTPOS); - int curLine = m_sciInput->Call(SCI_LINEFROMPOSITION, curPos); - int prevIndent = m_sciInput->Call(SCI_GETLINEINDENTATION, curLine - 1); + intptr_t curPos = m_sciInput->Call(SCI_GETCURRENTPOS); + intptr_t curLine = m_sciInput->Call(SCI_LINEFROMPOSITION, curPos); + intptr_t prevIndent = m_sciInput->Call(SCI_GETLINEINDENTATION, curLine - 1); m_sciInput->Call(SCI_SETLINEINDENTATION, curLine, prevIndent); curPos = m_sciInput->Call(SCI_GETLINEINDENTPOSITION, curLine); m_sciInput->Call(SCI_SETEMPTYSELECTION, curPos); } void LuaConsole::braceMatch() { - int curPos = m_sciInput->Call(SCI_GETCURRENTPOS); - int bracePos = INVALID_POSITION; + intptr_t curPos = m_sciInput->Call(SCI_GETCURRENTPOS); + intptr_t bracePos = INVALID_POSITION; // Check on both sides - if (isBrace(m_sciInput->Call(SCI_GETCHARAT, curPos - 1))) { + if (isBrace(static_cast(m_sciInput->Call(SCI_GETCHARAT, curPos - 1)))) { bracePos = curPos - 1; } - else if (isBrace(m_sciInput->Call(SCI_GETCHARAT, curPos))) { + else if (isBrace(static_cast(m_sciInput->Call(SCI_GETCHARAT, curPos)))) { bracePos = curPos; } // See if we are next to a brace if (bracePos != INVALID_POSITION) { - int otherPos = m_sciInput->Call(SCI_BRACEMATCH, bracePos, 0); + intptr_t otherPos = m_sciInput->Call(SCI_BRACEMATCH, bracePos, 0); if (otherPos != INVALID_POSITION) { m_sciInput->Call(SCI_BRACEHIGHLIGHT, bracePos, otherPos); } @@ -354,15 +354,15 @@ void LuaConsole::braceMatch() { void LuaConsole::showAutoCompletion() { std::string partialWord; - int curPos = m_sciInput->Call(SCI_GETCURRENTPOS); - int prevCh = m_sciInput->Call(SCI_GETCHARAT, curPos - 1); + intptr_t curPos = m_sciInput->Call(SCI_GETCURRENTPOS); + int prevCh = static_cast(m_sciInput->Call(SCI_GETCHARAT, curPos - 1)); // The cursor could be at the end of a partial word e.g. editor.Sty| if (isalpha(prevCh) || prevCh == '_') { partialWord = getWordAt(m_sciInput, curPos - 1); // Back up past the partial word - prevCh = m_sciInput->Call(SCI_GETCHARAT, curPos - 1 - partialWord.size()); + prevCh = static_cast(m_sciInput->Call(SCI_GETCHARAT, curPos - 1 - partialWord.size())); curPos = curPos - static_cast(partialWord.size()); } diff --git a/src/LuaScript.cpp b/src/LuaScript.cpp index 9730afd..55dc241 100644 --- a/src/LuaScript.cpp +++ b/src/LuaScript.cpp @@ -299,13 +299,13 @@ extern "C" __declspec(dllexport) void beNotified(SCNotification *notifyCode) { // Copied from SciTE GUI::ScintillaWindow wEditor; wEditor.SetID(curScintilla); - int lineEndStyled = wEditor.Call(SCI_LINEFROMPOSITION, wEditor.Call(SCI_GETENDSTYLED)); - int endStyled = wEditor.Call(SCI_POSITIONFROMLINE, lineEndStyled); + intptr_t lineEndStyled = wEditor.Call(SCI_LINEFROMPOSITION, wEditor.Call(SCI_GETENDSTYLED)); + intptr_t endStyled = wEditor.Call(SCI_POSITIONFROMLINE, lineEndStyled); StyleWriter styler(wEditor); int styleStart = 0; if (endStyled > 0) styleStart = styler.StyleAt(endStyled - 1); - styler.SetCodePage(wEditor.Call(SCI_GETCODEPAGE)); - LuaExtension::Instance().OnStyle(endStyled, static_cast(notifyCode->position) - endStyled, styleStart, &styler); + styler.SetCodePage(static_cast(wEditor.Call(SCI_GETCODEPAGE))); + LuaExtension::Instance().OnStyle(endStyled, notifyCode->position - endStyled, styleStart, &styler); styler.Flush(); break; } diff --git a/src/Npp/Sci_Position.h b/src/Npp/Sci_Position.h index abd0f34..88ad513 100644 --- a/src/Npp/Sci_Position.h +++ b/src/Npp/Sci_Position.h @@ -18,7 +18,7 @@ typedef ptrdiff_t Sci_Position; typedef size_t Sci_PositionU; // For Sci_CharacterRange which is defined as long to be compatible with Win32 CHARRANGE -typedef long Sci_PositionCR; +typedef intptr_t Sci_PositionCR; #ifdef _WIN32 #define SCI_METHOD __stdcall diff --git a/src/SciTE/GUI.h b/src/SciTE/GUI.h index dfb3886..32f539a 100644 --- a/src/SciTE/GUI.h +++ b/src/SciTE/GUI.h @@ -153,7 +153,7 @@ class ScintillaWindow : public Window { bool CanCall() const { return wid && fn && ptr; } - int Call(unsigned int msg, uptr_t wParam=0, sptr_t lParam=0) { + intptr_t Call(unsigned int msg, uptr_t wParam=0, sptr_t lParam=0) { switch (msg) { case SCI_CREATEDOCUMENT: case SCI_CREATELOADER: @@ -179,10 +179,10 @@ class ScintillaWindow : public Window { throw ScintillaFailure(status); return retVal; } - int CallPointer(unsigned int msg, uptr_t wParam, void *s) { + intptr_t CallPointer(unsigned int msg, uptr_t wParam, void *s) { return Call(msg, wParam, reinterpret_cast(s)); } - int CallString(unsigned int msg, uptr_t wParam, const char *s) { + intptr_t CallString(unsigned int msg, uptr_t wParam, const char *s) { return Call(msg, wParam, reinterpret_cast(s)); } sptr_t Send(unsigned int msg, uptr_t wParam=0, sptr_t lParam=0); diff --git a/src/SciTE/LuaExtension.cpp b/src/SciTE/LuaExtension.cpp index 7a17b56..47b8c4d 100644 --- a/src/SciTE/LuaExtension.cpp +++ b/src/SciTE/LuaExtension.cpp @@ -1896,15 +1896,15 @@ void LuaExtension::CallShortcut(int id) { // Similar to StyleContext class in Scintilla struct StylingContext { - unsigned int startPos; - int lengthDoc; + uintptr_t startPos; + intptr_t lengthDoc; int initStyle; StyleWriter *styler; - unsigned int endPos; - unsigned int endDoc; + uintptr_t endPos; + uintptr_t endDoc; - unsigned int currentPos; + uintptr_t currentPos; bool atLineStart; bool atLineEnd; int state; @@ -1920,7 +1920,7 @@ struct StylingContext { } void Colourize() { - int end = currentPos - 1; + intptr_t end = currentPos - 1; if (end >= static_cast(endDoc)) end = static_cast(endDoc)-1; styler->ColourTo(end, state); @@ -1981,7 +1981,7 @@ struct StylingContext { void GetNextChar() { lenCurrent = lenNext; lenNext = 1; - int nextPos = currentPos + lenCurrent; + intptr_t nextPos = currentPos + lenCurrent; unsigned char byteNext = static_cast(styler->SafeGetCharAt(nextPos)); unsigned int nextSlot = (cursorPos + 1) % 3; memcpy(cursor[nextSlot], "\0\0\0\0\0\0\0\0", 8); @@ -2149,13 +2149,13 @@ struct StylingContext { static int Token(lua_State *L) { StylingContext *context = Context(L); - int start = context->styler->GetStartSegment(); - int end = context->currentPos - 1; - int len = end - start + 1; + intptr_t start = context->styler->GetStartSegment(); + intptr_t end = context->currentPos - 1; + intptr_t len = end - start + 1; if (len <= 0) len = 1; char *sReturn = new char[len + 1]; - for (int i = 0; i < len; i++) { + for (intptr_t i = 0; i < len; i++) { sReturn[i] = context->styler->SafeGetCharAt(start + i); } sReturn[len] = '\0'; @@ -2187,7 +2187,7 @@ struct StylingContext { } }; -bool LuaExtension::OnStyle(unsigned int startPos, int lengthDoc, int initStyle, StyleWriter *styler) { +bool LuaExtension::OnStyle(uintptr_t startPos, intptr_t lengthDoc, int initStyle, StyleWriter *styler) { if (luaState) { lua_pushstring(luaState, "Npp_Callbacks"); lua_gettable(luaState, LUA_REGISTRYINDEX); diff --git a/src/SciTE/LuaExtension.h b/src/SciTE/LuaExtension.h index b6e1d8e..ec805a9 100644 --- a/src/SciTE/LuaExtension.h +++ b/src/SciTE/LuaExtension.h @@ -55,7 +55,7 @@ class LuaExtension final { void CallShortcut(int id); // Scintilla callbacks - bool OnStyle(unsigned int startPos, int lengthDoc, int initStyle, StyleWriter *styler); + bool OnStyle(uintptr_t startPos, intptr_t lengthDoc, int initStyle, StyleWriter *styler); bool OnChar(const SCNotification *sc); bool OnSavePointReached(const SCNotification *sc); bool OnSavePointLeft(const SCNotification *sc); diff --git a/src/SciTE/StyleWriter.cpp b/src/SciTE/StyleWriter.cpp index fd9b8ed..0f18fd3 100644 --- a/src/SciTE/StyleWriter.cpp +++ b/src/SciTE/StyleWriter.cpp @@ -24,7 +24,7 @@ bool TextReader::InternalIsLeadByte(char ch) const { return GUI::IsDBCSLeadByte(codePage, ch); } -void TextReader::Fill(int position) { +void TextReader::Fill(intptr_t position) { if (lenDoc == -1) lenDoc = sw.Call(SCI_GETTEXTLENGTH, 0, 0); startPos = position - slopSize; @@ -48,30 +48,30 @@ bool TextReader::Match(int pos, const char *s) { return true; } -int TextReader::StyleAt(int position) { +int TextReader::StyleAt(intptr_t position) { return static_cast(sw.Call(SCI_GETSTYLEAT, position, 0)); } -int TextReader::GetLine(int position) { +intptr_t TextReader::GetLine(intptr_t position) { return sw.Call(SCI_LINEFROMPOSITION, position, 0); } -int TextReader::LineStart(int line) { +intptr_t TextReader::LineStart(intptr_t line) { return sw.Call(SCI_POSITIONFROMLINE, line, 0); } -int TextReader::LevelAt(int line) { - return sw.Call(SCI_GETFOLDLEVEL, line, 0); +int TextReader::LevelAt(intptr_t line) { + return static_cast(sw.Call(SCI_GETFOLDLEVEL, line, 0)); } -int TextReader::Length() { +intptr_t TextReader::Length() { if (lenDoc == -1) lenDoc = sw.Call(SCI_GETTEXTLENGTH, 0, 0); return lenDoc; } -int TextReader::GetLineState(int line) { - return sw.Call(SCI_GETLINESTATE, line); +int TextReader::GetLineState(intptr_t line) { + return static_cast(sw.Call(SCI_GETLINESTATE, line)); } StyleWriter::StyleWriter(GUI::ScintillaWindow &sw_) : @@ -81,19 +81,19 @@ StyleWriter::StyleWriter(GUI::ScintillaWindow &sw_) : styleBuf[0] = 0; } -int StyleWriter::SetLineState(int line, int state) { - return sw.Call(SCI_SETLINESTATE, line, state); +int StyleWriter::SetLineState(intptr_t line, int state) { + return static_cast(sw.Call(SCI_SETLINESTATE, line, state)); } -void StyleWriter::StartAt(unsigned int start, char chMask) { +void StyleWriter::StartAt(uintptr_t start, char chMask) { sw.Call(SCI_STARTSTYLING, start, chMask); } -void StyleWriter::StartSegment(unsigned int pos) { +void StyleWriter::StartSegment(uintptr_t pos) { startSeg = pos; } -void StyleWriter::ColourTo(unsigned int pos, int chAttr) { +void StyleWriter::ColourTo(uintptr_t pos, int chAttr) { // Only perform styling if non empty range if (pos != startSeg - 1) { if (validLen + (pos - startSeg + 1) >= bufferSize) @@ -102,7 +102,7 @@ void StyleWriter::ColourTo(unsigned int pos, int chAttr) { // Too big for buffer so send directly sw.Call(SCI_SETSTYLING, pos - startSeg + 1, chAttr); } else { - for (unsigned int i = startSeg; i <= pos; i++) { + for (uintptr_t i = startSeg; i <= pos; i++) { styleBuf[validLen++] = static_cast(chAttr); } } @@ -110,7 +110,7 @@ void StyleWriter::ColourTo(unsigned int pos, int chAttr) { startSeg = pos+1; } -void StyleWriter::SetLevel(int line, int level) { +void StyleWriter::SetLevel(intptr_t line, int level) { sw.Call(SCI_SETFOLDLEVEL, line, level); } diff --git a/src/SciTE/StyleWriter.h b/src/SciTE/StyleWriter.h index 440299a..9acb8e1 100644 --- a/src/SciTE/StyleWriter.h +++ b/src/SciTE/StyleWriter.h @@ -19,17 +19,17 @@ class TextReader { * and retrieval overhead. * @a slopSize positions the buffer before the desired position * in case there is some backtracking. */ - enum {bufferSize=4000, slopSize=bufferSize/8}; + enum : intptr_t {bufferSize=4000, slopSize=bufferSize/8}; char buf[bufferSize+1]; - int startPos; - int endPos; + intptr_t startPos; + intptr_t endPos; int codePage; GUI::ScintillaWindow &sw; - int lenDoc; + intptr_t lenDoc; bool InternalIsLeadByte(char ch) const; - void Fill(int position); + void Fill(intptr_t position); public: explicit TextReader(GUI::ScintillaWindow &sw_); char operator[](int position) { @@ -39,7 +39,7 @@ class TextReader { return buf[position - startPos]; } /** Safe version of operator[], returning a defined value for invalid position. */ - char SafeGetCharAt(int position, char chDefault=' ') { + char SafeGetCharAt(intptr_t position, char chDefault=' ') { if (position < startPos || position >= endPos) { Fill(position); if (position < startPos || position >= endPos) { @@ -56,12 +56,12 @@ class TextReader { codePage = codePage_; } bool Match(int pos, const char *s); - int StyleAt(int position); - int GetLine(int position); - int LineStart(int line); - int LevelAt(int line); - int Length(); - int GetLineState(int line); + int StyleAt(intptr_t position); + intptr_t GetLine(intptr_t position); + intptr_t LineStart(intptr_t line); + int LevelAt(intptr_t line); + intptr_t Length(); + int GetLineState(intptr_t line); }; // Adds methods needed to write styles and folding @@ -72,17 +72,17 @@ class StyleWriter : public TextReader { protected: char styleBuf[bufferSize]; int validLen; - unsigned int startSeg; + uintptr_t startSeg; public: explicit StyleWriter(GUI::ScintillaWindow &sw_); void Flush(); - int SetLineState(int line, int state); + int SetLineState(intptr_t line, int state); - void StartAt(unsigned int start, char chMask=31); - unsigned int GetStartSegment() const { return startSeg; } - void StartSegment(unsigned int pos); - void ColourTo(unsigned int pos, int chAttr); - void SetLevel(int line, int level); + void StartAt(uintptr_t start, char chMask=31); + uintptr_t GetStartSegment() const { return startSeg; } + void StartSegment(uintptr_t pos); + void ColourTo(uintptr_t pos, int chAttr); + void SetLevel(intptr_t line, int level); }; #endif