當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Map.AddMapping方法代碼示例

本文整理匯總了Golang中github.com/neelance/sourcemap.Map.AddMapping方法的典型用法代碼示例。如果您正苦於以下問題:Golang Map.AddMapping方法的具體用法?Golang Map.AddMapping怎麽用?Golang Map.AddMapping使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/neelance/sourcemap.Map的用法示例。


在下文中一共展示了Map.AddMapping方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: WriteCommandPackage

func (s *Session) WriteCommandPackage(pkg *PackageData, pkgObj string) error {
	if !pkg.IsCommand() || pkg.UpToDate {
		return nil
	}

	if err := os.MkdirAll(filepath.Dir(pkgObj), 0777); err != nil {
		return err
	}
	codeFile, err := os.Create(pkgObj)
	if err != nil {
		return err
	}
	defer codeFile.Close()

	sourceMapFilter := &compiler.SourceMapFilter{Writer: codeFile}
	if s.options.CreateMapFile {
		m := sourcemap.Map{File: filepath.Base(pkgObj)}
		mapFile, err := os.Create(pkgObj + ".map")
		if err != nil {
			return err
		}

		defer func() {
			m.WriteTo(mapFile)
			mapFile.Close()
			fmt.Fprintf(codeFile, "//# sourceMappingURL=%s.map\n", filepath.Base(pkgObj))
		}()

		sourceMapFilter.MappingCallback = func(generatedLine, generatedColumn int, fileSet *token.FileSet, originalPos token.Pos) {
			if !originalPos.IsValid() {
				m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn})
				return
			}
			pos := fileSet.Position(originalPos)
			file := pos.Filename
			switch {
			case strings.HasPrefix(file, s.options.GOPATH):
				file = filepath.ToSlash(filepath.Join("/gopath", file[len(s.options.GOPATH):]))
			case strings.HasPrefix(file, s.options.GOROOT):
				file = filepath.ToSlash(filepath.Join("/goroot", file[len(s.options.GOROOT):]))
			default:
				file = filepath.Base(file)
			}
			m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn, OriginalFile: file, OriginalLine: pos.Line, OriginalColumn: pos.Column})
		}
	}

	deps, err := s.ImportDependencies(pkg.Archive)
	if err != nil {
		return err
	}
	compiler.WriteProgramCode(deps, s.ImportContext, sourceMapFilter)

	return nil
}
開發者ID:phaikawl,項目名稱:gopherjs,代碼行數:55,代碼來源:build.go

示例2: writeCommandPackage

func (s *session) writeCommandPackage(pkg *packageData, pkgObj string) error {
	if !pkg.IsCommand() || pkg.UpToDate {
		return nil
	}

	if err := os.MkdirAll(filepath.Dir(pkgObj), 0777); err != nil {
		return err
	}
	codeFile, err := os.Create(pkgObj)
	if err != nil {
		return err
	}
	defer codeFile.Close()
	mapFile, err := os.Create(pkgObj + ".map")
	if err != nil {
		return err
	}
	defer mapFile.Close()

	var allPkgs []*translator.Archive
	for _, depPath := range pkg.Archive.Dependencies {
		dep, err := s.importPackage(depPath)
		if err != nil {
			return err
		}
		allPkgs = append(allPkgs, dep)
	}

	m := sourcemap.Map{File: filepath.Base(pkgObj)}
	s.t.WriteProgramCode(allPkgs, pkg.ImportPath, &translator.SourceMapFilter{Writer: codeFile, MappingCallback: func(generatedLine, generatedColumn int, fileSet *token.FileSet, originalPos token.Pos) {
		if !originalPos.IsValid() {
			m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn})
			return
		}
		pos := fileSet.Position(originalPos)
		file := pos.Filename
		switch {
		case strings.HasPrefix(file, build.Default.GOPATH):
			file = filepath.ToSlash(filepath.Join("/gopath", file[len(build.Default.GOPATH):]))
		case strings.HasPrefix(file, build.Default.GOROOT):
			file = filepath.ToSlash(filepath.Join("/goroot", file[len(build.Default.GOROOT):]))
		default:
			file = filepath.Base(file)
		}
		m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn, OriginalFile: file, OriginalLine: pos.Line, OriginalColumn: pos.Column})
	}})
	fmt.Fprintf(codeFile, "//# sourceMappingURL=%s.map\n", filepath.Base(pkgObj))
	m.WriteTo(mapFile)

	return nil
}
開發者ID:hajimehoshi,項目名稱:gopherjs,代碼行數:51,代碼來源:tool.go

示例3: NewMappingCallback

func NewMappingCallback(m *sourcemap.Map, goroot, gopath string) func(generatedLine, generatedColumn int, originalPos token.Position) {
	return func(generatedLine, generatedColumn int, originalPos token.Position) {
		if !originalPos.IsValid() {
			m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn})
			return
		}
		file := originalPos.Filename
		switch hasGopathPrefix, prefixLen := hasGopathPrefix(file, gopath); {
		case hasGopathPrefix:
			file = filepath.ToSlash(file[prefixLen+4:])
		case strings.HasPrefix(file, goroot):
			file = filepath.ToSlash(file[len(goroot)+4:])
		default:
			file = filepath.Base(file)
		}
		m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn, OriginalFile: file, OriginalLine: originalPos.Line, OriginalColumn: originalPos.Column})
	}
}
開發者ID:flying99999,項目名稱:gopherjs,代碼行數:18,代碼來源:build.go


注:本文中的github.com/neelance/sourcemap.Map.AddMapping方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。