-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Support system/user default clone editors #22378
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a0bd584
Support default clone editor
lunny 46a0375
fix lint
lunny c14cd96
Merge branch 'main' into lunny/default_editor
lunny 1c717e9
User could select his favorate editor
lunny efcfc96
fix lint
lunny e2717c1
Allow chose multiple editors
lunny ed38cdc
Merge branch 'main' into lunny/default_editor
lunny 58a6a08
fix unnecessary argument
lunny 5b92f63
Fix lint
lunny 11c74a6
Fix typo
lunny b555176
Apply suggestions from code review
lunny dcb8580
improve code
lunny 60a9132
Merge branch 'main' into lunny/default_editor
lunny 1dfcab4
merge main branch
lunny 945879f
merge main branch
lunny 9b1f268
merge main branch
lunny 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
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
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 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
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,125 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package dev | ||
|
||
import ( | ||
"html/template" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models/system" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
const KeyDevDefaultEditor = "dev.default_editor" | ||
|
||
type Editor struct { | ||
Name string | ||
URL string | ||
Icon string | ||
} | ||
|
||
func (e *Editor) RenderURL(repoURL string) template.URL { | ||
return template.URL(strings.ReplaceAll(e.URL, "${repo_url}", repoURL)) | ||
} | ||
|
||
var defaultEditors = []Editor{ | ||
{ | ||
Name: "VS Code", | ||
URL: "vscode://vscode.git/clone?url=${repo_url}", | ||
Icon: `gitea-vscode`, | ||
}, | ||
{ | ||
Name: "VSCodium", | ||
URL: "vscodium://vscode.git/clone?url=${repo_url}", | ||
Icon: `gitea-vscodium`, | ||
}, | ||
} | ||
|
||
func GetEditorByName(name string) *Editor { | ||
for _, editor := range defaultEditors { | ||
if editor.Name == name { | ||
return &editor | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// GetEditors returns all editors | ||
func GetEditors() ([]Editor, error) { | ||
return defaultEditors, nil | ||
} | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func DefaultEditorName() string { | ||
return defaultEditors[0].Name | ||
} | ||
|
||
func GetDefaultEditor() (*Editor, error) { | ||
defaultName, err := system.GetSetting(KeyDevDefaultEditor) | ||
if err != nil && !system.IsErrSettingIsNotExist(err) { | ||
return nil, err | ||
} | ||
for _, editor := range defaultEditors { | ||
if editor.Name == defaultName { | ||
return &editor, nil | ||
} | ||
} | ||
return &defaultEditors[0], nil | ||
} | ||
|
||
func SetDefaultEditor(name string) error { | ||
for _, editor := range defaultEditors { | ||
if editor.Name == name { | ||
return system.SetSetting(&system.Setting{ | ||
SettingKey: KeyDevDefaultEditor, | ||
SettingValue: name, | ||
}) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type ErrUnknownEditor struct { | ||
editorName string | ||
} | ||
|
||
func (e ErrUnknownEditor) Error() string { | ||
return "Unknown editor: " + e.editorName | ||
} | ||
|
||
func GetUserDefaultEditor(userID int64) (*Editor, error) { | ||
defaultName, err := user_model.GetSetting(userID, KeyDevDefaultEditor) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, editor := range defaultEditors { | ||
if editor.Name == defaultName { | ||
return &editor, nil | ||
} | ||
} | ||
return nil, ErrUnknownEditor{defaultName} | ||
} | ||
|
||
func SetUserDefaultEditor(userID int64, name string) error { | ||
return user_model.SetUserSetting(userID, KeyDevDefaultEditor, name) | ||
} | ||
|
||
func GetUserDefaultEditorWithFallback(user *user_model.User) (*Editor, error) { | ||
if user == nil || user.ID <= 0 { | ||
return GetDefaultEditor() | ||
} | ||
editor, err := GetUserDefaultEditor(user.ID) | ||
if err == nil { | ||
return editor, nil | ||
} | ||
|
||
if theErr, ok := err.(ErrUnknownEditor); ok { | ||
log.Error("Unknown editor for user %d: %s, fallback to system default", user.ID, theErr.editorName) | ||
return GetDefaultEditor() | ||
} | ||
if user_model.IsErrUserSettingIsNotExist(err) { | ||
return GetDefaultEditor() | ||
} | ||
return nil, err | ||
} |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.