-
Notifications
You must be signed in to change notification settings - Fork 58
Case insensitive file open filters #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
embarc-gabriel
wants to merge
3
commits into
musescore:main
Choose a base branch
from
embarc-gabriel:case-insensitive-file-open-filters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * SPDX-License-Identifier: GPL-3.0-only | ||
| * MuseScore-CLA-applies | ||
| * | ||
| * MuseScore Studio | ||
| * Music Composition & Notation | ||
| * | ||
| * Copyright (C) 2026 MuseScore Limited and others | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 3 as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include "filedialogfilters.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cctype> | ||
| #include <iterator> | ||
| #include <optional> | ||
| #include <sstream> | ||
| #include <vector> | ||
|
|
||
| #include "global/stringutils.h" | ||
|
|
||
| using namespace muse::interactive; | ||
|
|
||
| namespace { | ||
| struct NameFilterParts { | ||
| std::string description; | ||
| std::string globs; | ||
| }; | ||
|
|
||
| std::optional<NameFilterParts> splitNameFilter(const std::string& filter) | ||
| { | ||
| const bool endsWithGroup = !filter.empty() && filter.back() == ')'; | ||
| const size_t open = endsWithGroup ? filter.rfind('(') : std::string::npos; | ||
|
|
||
| if (open == std::string::npos) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| return NameFilterParts { filter.substr(0, open), filter.substr(open + 1, filter.size() - open - 2) }; | ||
| } | ||
|
|
||
| std::vector<std::string> splitGlobs(const std::string& globs) | ||
| { | ||
| std::istringstream in(globs); | ||
| return { std::istream_iterator<std::string>(in), std::istream_iterator<std::string>() }; | ||
| } | ||
|
|
||
| std::vector<std::string> tokenizeGlob(const std::string& glob) | ||
| { | ||
| std::vector<std::string> tokens; | ||
|
|
||
| for (size_t pos = 0; pos < glob.size();) { | ||
| const size_t close = glob[pos] == '[' ? glob.find(']', pos) : std::string::npos; | ||
| const size_t length = close == std::string::npos ? 1 : close - pos + 1; | ||
| tokens.push_back(glob.substr(pos, length)); | ||
| pos += length; | ||
| } | ||
|
|
||
| return tokens; | ||
| } | ||
|
|
||
| bool isPlainLetter(const std::string& token) | ||
| { | ||
| return token.size() == 1 && std::isalpha(static_cast<unsigned char>(token.front())); | ||
| } | ||
|
|
||
| std::string bothCases(const std::string& letter) | ||
| { | ||
| const unsigned char c = static_cast<unsigned char>(letter.front()); | ||
| return { '[', static_cast<char>(std::tolower(c)), static_cast<char>(std::toupper(c)), ']' }; | ||
| } | ||
|
|
||
| bool needsCaseInsensitiveRewrite(const std::string& glob) | ||
| { | ||
| return std::ranges::any_of(tokenizeGlob(glob), isPlainLetter); | ||
| } | ||
|
|
||
| std::string caseInsensitiveGlobIfNeeded(const std::string& glob) | ||
| { | ||
| return needsCaseInsensitiveRewrite(glob) ? caseInsensitiveGlob(glob) : glob; | ||
| } | ||
| } | ||
|
|
||
| std::string muse::interactive::caseInsensitiveGlob(const std::string& glob) | ||
| { | ||
| std::string result; | ||
| for (const std::string& token : tokenizeGlob(glob)) { | ||
| result += isPlainLetter(token) ? bothCases(token) : token; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| std::string muse::interactive::caseInsensitiveNameFilter(const std::string& filter) | ||
| { | ||
| const std::optional<NameFilterParts> parts = splitNameFilter(filter); | ||
| if (!parts) { | ||
| return filter; | ||
| } | ||
|
|
||
| std::vector<std::string> globs = splitGlobs(parts->globs); | ||
| std::ranges::transform(globs, globs.begin(), caseInsensitiveGlobIfNeeded); | ||
|
|
||
| return parts->description + '(' + muse::strings::join(globs, " ") + ')'; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * SPDX-License-Identifier: GPL-3.0-only | ||
| * MuseScore-CLA-applies | ||
| * | ||
| * MuseScore Studio | ||
| * Music Composition & Notation | ||
| * | ||
| * Copyright (C) 2026 MuseScore Limited and others | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 3 as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace muse::interactive { | ||
| std::string caseInsensitiveGlob(const std::string& glob); | ||
| std::string caseInsensitiveNameFilter(const std::string& filter); | ||
| } |
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
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
113 changes: 113 additions & 0 deletions
113
framework/interactive/tests/filedialogfilters_tests.cpp
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * SPDX-License-Identifier: GPL-3.0-only | ||
| * MuseScore-CLA-applies | ||
| * | ||
| * MuseScore Studio | ||
| * Music Composition & Notation | ||
| * | ||
| * Copyright (C) 2026 MuseScore Limited and others | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 3 as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "interactive/internal/filedialogfilters.h" | ||
|
|
||
| using namespace muse::interactive; | ||
|
|
||
| class Interactive_FileDialogFiltersTests : public ::testing::Test | ||
| { | ||
| }; | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveGlob_ExpandsLetters) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveGlob("*.mp3"), "*.[mM][pP]3"); | ||
| EXPECT_EQ(caseInsensitiveGlob("*.3gp"), "*.3[gG][pP]"); | ||
| EXPECT_EQ(caseInsensitiveGlob("*.film_cpk"), "*.[fF][iI][lL][mM]_[cC][pP][kK]"); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveGlob_NormalisesUpperCaseInput) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveGlob("*.MTV"), "*.[mM][tT][vV]"); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveGlob_LeavesNonLettersUntouched) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveGlob("*"), "*"); | ||
| EXPECT_EQ(caseInsensitiveGlob("*.302"), "*.302"); | ||
| EXPECT_EQ(caseInsensitiveGlob(""), ""); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveGlob_LeavesBracketExpressionsUntouched) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveGlob("*.[mM][pP]3"), "*.[mM][pP]3"); | ||
| EXPECT_EQ(caseInsensitiveGlob("*.[0-9]"), "*.[0-9]"); | ||
| EXPECT_EQ(caseInsensitiveGlob("[a-z]*"), "[a-z]*"); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveGlob_ExpandsLettersAroundBracketExpressions) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveGlob("*.m[34]a"), "*.[mM][34][aA]"); | ||
| EXPECT_EQ(caseInsensitiveGlob("*.[mM]p[34]"), "*.[mM][pP][34]"); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveNameFilter_SingleGroup) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveNameFilter("Audio files (*.mp3 *.wav)"), | ||
| "Audio files (*.[mM][pP]3 *.[wW][aA][vV])"); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveNameFilter_GlobWithBracketExpression) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveNameFilter("MPEG-4 audio (*.m4a *.m[34]a)"), | ||
| "MPEG-4 audio (*.[mM]4[aA] *.[mM][34][aA])"); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveNameFilter_RewritesOnlyTrailingGroup) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveNameFilter("All supported files (*.mp3,*.aac, ...) (*.aac *.ac3 *.mp2)"), | ||
| "All supported files (*.mp3,*.aac, ...) (*.[aA][aA][cC] *.[aA][cC]3 *.[mM][pP]2)"); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveNameFilter_AllFiles) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveNameFilter("All files (*)"), "All files (*)"); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveNameFilter_EmptyGroup) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveNameFilter("Name ()"), "Name ()"); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveNameFilter_CollapsesWhitespaceBetweenGlobs) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveNameFilter("Audio (*.mp3 *.wav )"), "Audio (*.[mM][pP]3 *.[wW][aA][vV])"); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveNameFilter_LeavesBareGlobListUntouched) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveNameFilter("*.mp3 *.wav"), "*.mp3 *.wav"); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveNameFilter_EmptyString) | ||
| { | ||
| EXPECT_EQ(caseInsensitiveNameFilter(""), ""); | ||
| } | ||
|
|
||
| TEST_F(Interactive_FileDialogFiltersTests, CaseInsensitiveNameFilter_IsIdempotent) | ||
| { | ||
| const std::string once = caseInsensitiveNameFilter("Audio files (*.mp3 *.wav *.m[34]a)"); | ||
| EXPECT_EQ(caseInsensitiveNameFilter(once), once); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: musescore/muse_framework
Length of output: 5381
🏁 Script executed:
Repository: musescore/muse_framework
Length of output: 50380
🏁 Script executed:
Repository: musescore/muse_framework
Length of output: 18474
🏁 Script executed:
Repository: musescore/muse_framework
Length of output: 44942
Apply the case-insensitive conversion to the asynchronous Linux open path.
Interactive::selectOpeningFilehas no options parameter, and its Linux branch callsmakeSelectFileQuery(..., filter)with the helper default of0. No intermediate layer suppliesQFileDialog::HideNameFilterDetails, somatchCaseInsensitivelyremains false and uppercase extensions can remain unrecognized. Apply the conversion in this path or add an explicit option contract.🤖 Prompt for AI Agents